我有一个LINQ查询的情况。它有两个连接(一对多),但它会带回连接表中的所有列。我不确定如何创建LINQ查询,以便只从连接的表中返回几个字段。
var data = from mc in ctx.MembershipChapters
where mc.PartitionKey == controllerStateManager.PartitionKey && mc.MembershipId == membershipId
join prd in ctx.Products on mc.ProductId
equals prd.Id into prods
from prd in prods.DefaultIfEmpty()
join oli in ctx.OrderLineItems on mc.OrderLineItemId equals oli.OrderLineItemId into olis
from oli in olis.DefaultIfEmpty()
select new
{
MembershipName = mc.Membership.Name,
Products = prods.Select(p => new {
ProductName = p.Name, ProductId = p.Id }),
OrderLineItems = olis.Select(o => new { OrderLineItemName = o.Description, OrderLineItemId = o.OrderLineItemId })
};
controllerStateManager.Data = data.ToList();
这不管用。。。我得到一个错误:“o”不在范围内。
基本上输出应该如下:
会员资格
我是LINQ的新手,我已经为此奋斗了太久。
这是一个有点难读,但如果域链接正确,那么我想你只是想结束这样的查询:
from ol in ctx.OrderLines where
ol.MembershipChapter.PartitionKey == controllerStateManager.PartitionKey
select new {ol.Whatever, ol.Product.Whatever};