【问题标题】:Linq Group by, Concatenate and load child entities on a listLinq Group by,连接并在列表中加载子实体
【发布时间】:2014-01-29 22:15:39
【问题描述】:

我将稍微简化一下我的模型,以展示我想要做什么。我完全知道如何使用 SQL 但我遇到了 EF 和 LINQ 的问题。

抱歉,帖子太长了。

我有下一个 POCO:

public class Order
{
   public int Id {get; set;}
   public double Quantity {get; set;}   
   public List<Status> StatusChanges{get; set;}
}
public class Status
{
   public int Id {get; set;}
   public DateTime StatusDate {get; set;}
   public StatusType StatusType  {get; set;}
}
public class StatusType 
{
   public int Id {get; set;}
   public string Code {get; set;}
   public string Description {get; get;}
}

一个Order 有多个Status 而一个Status 只有一个StatusType

我的问题:

1) 如何在 LINQ 查询中选择一个列表,其中包含加载了 StatusType 的所有订单

我试试

var results= (from o in db.Orders
            select new {o.Id,o.Quantity,o.StatusChanges})
            .Include(o=>o.StatusChanges.Select(s=>s.StatusType));

但这不是我想要的,我想要一个包含 OrderStatusStatusType 的列表(即使 Order 信息是重复的)我知道我可以做一个双 foreach 但我会想知道是否是单查询方式(或双查询一个到数据库,另一个使用 linq 到实体)

2) 如何创建一个列表,其中包含Order Id 和所有连接状态的代码。示例

订单:

Id    Quantity
1       2

状态

Id  StatusDate   StatusTypeId
1    01-10-2014       1
2    02-10-2014       2

状态类型

Id   Code  Description
1      E    Entered
2      C    Confirmed
3      D    Deleted

结果:

OrderId  Quantity    StatusResume
1         2           E,C

我知道我可以使用这个question,但我不知道如何访问列表中加载的元素。

【问题讨论】:

    标签: linq entity-framework linq-to-entities ef-code-first


    【解决方案1】:

    尝试以下查询:

    1) 如何在 LINQ 查询中选择一个列表,其中包含加载了 StatusType 的所有订单

    var results = (from o in db.Orders
                   from s in o.StatusChanges
                   select new
                       {
                           OrderId = o.Id,
                           Quantity = o.Quantity,
                           StatusId = s.Id,
                           StatusDate = s.StatusDate,
                           StatusCode = s.StatusType != null ? s.StatusType.Code : null
                       }).ToList();
    

    2)如何制作一个包含订单ID和所有状态代码的列表

    var results = (from o in db.Orders
                           select new
                               {
                                   o.Id,
                                   o.Quantity,
                                   StatusCodes =
                               o.StatusChanges.Where(c => c.StatusType != null).Select(c => c.StatusType.Code)
                               }).ToList().Select(x => new
                                   {
                                       x.Id,
                                       x.Quantity,
                                       StatusResume = string.Join(",", x.StatusCodes)
                                   }).ToList();
    

    【讨论】:

    • 很抱歉,第二个不起作用。 LINQ to Entities 不支持Aggregate
    • @MarcinJuraszek:你是对的。完全忘记了 LINQ To 实体不支持聚合。将更新答案。
    • 谢谢两位!我真的不知道进行子咨询的技巧以及选择 LINQ to Entities 进行新咨询的可能性。
    【解决方案2】:

    Ilija Dimov 的回答中的第一个应该有效。第二个有点棘手,因为Aggregate 扩展method is not supported by LINQ to Entities。这就是为什么您必须将必要的数据下载到内存并在内存中执行字符串连接作为 LINQ to Objects 查询。

    var results = (from o in db.Orders
                   select new
                   {
                       o.Id,
                       o.Quantity,
                       o.StatusCodes.Select(c => c.StatusType.Code)
                   }).AsEnumerable()
                     .Select(x => new {
                                x.Id,
                                x.Quantity, 
                                StatusResume = string.Join(',', x.StatusCodes)
                             }).ToList();
                       }).ToList();
    

    【讨论】:

    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2017-06-08
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多