【问题标题】:LINQ group by on data serviceLINQ group by on 数据服务
【发布时间】:2014-01-17 06:57:14
【问题描述】:

我的第一个问题在这里:Linq lambda expression many to many table select

我在服务器端使用数据服务从客户端的数据库中检索数据。我知道数据服务不支持分组。

在客户端调用:

public List<Lottery> GetLotteriesByLotteryOfferId(int lotteryOfferId)
{
    var query = this.ClientRepositories.BackOfficeData.CreateQuery<Lottery>("GetLotteriesByLotteryOfferId")
                    .AddQueryOption("lotteryOfferId", lotteryOfferId).ToList();

    return query;
}

我在服务器端的 lambda 查询不起作用:

public IQueryable<Lottery> GetLotteriesByLotteryOfferId(int lotteryOfferId)
{
    return this.db.LotteryOffers
                                .Where(lo => lo.Id == lotteryOfferId)
                                .SelectMany(lo => lo.LotteryDrawDates)
                                .Select(ldd => ldd.Lottery)
                                .GroupBy(s => new { s.Name, s.CreatedBy, s.ModifiedOn, s.Id })
                                .Select(g => new Lottery
                                                {
                                                    Name = g.Key.Name,
                                                    CreatedBy = g.Key.CreatedBy,
                                                    ModifiedOn = g.Key.ModifiedOn,
                                                    Id = g.Key.Id
                                                });
}

这里出现错误:

无法在 LINQ 中构造实体或复杂类型“Lottery” 到实体查询。

我明白,因为 Group By。 我的问题是如何在客户端实现这一目标?所以我在服务器端运行查询,直到彩票选择(没有分组部分)并在客户端通过查询部分附加额外的组?

其他问题 如果我想使用自定义视图模型,我只需在客户端创建它并选择“viewModel”类型而不是 Lottery 选项?

例子:

                                .Select(g => new CustomViewModel
                                                {
                                                    CountRows = g.Count()
                                                });

【问题讨论】:

  • “客户端”是什么意思
  • 在我的客户端(webapp)我使用方法来调用服务方法。我将服务方法称为服务器端:)

标签: c# sql linq entity-framework lambda


【解决方案1】:

我认为错误在于您在选择器中使用了 Lottery 类。 LINQ to entity 只能构造 pur "Data Transfert Object" :类只包含具有普通 getter 和 setter 且没有构造函数的公共属性。

class LotteryDTO
{
  public string Name { get; set; }
  public string CreatedBy { get; set; } 
  ...
}

IQueryable<LotteryDTO> result = db.LotteryOffers
                            .Where(...)
                            .SelectMany(...)
                            .Select(...)
                            .GroupBy(...)
                            .Select(g => new LotteryDTO {
                               Name = g.Key.Name,
                               CreatedBy = g.Key.CreatedBy,
                               ...    
                            });

【讨论】:

    【解决方案2】:

    我认为错误是 LINQ to Entities 无法投影到自定义类中,您应该可以通过在投影之前添加 AsEnumerable 来做到这一点。

    public IQueryable<Lottery> GetLotteriesByLotteryOfferId(int lotteryOfferId)
    {
        return this.db.LotteryOffers
                                    .Where(lo => lo.Id == lotteryOfferId)
                                    .SelectMany(lo => lo.LotteryDrawDates)
                                    .Select(ldd => ldd.Lottery)
                                    .GroupBy(s => new { s.Name, s.CreatedBy, s.ModifiedOn, s.Id })
                                    .AsEnumerable()
                                    .Select(g => new Lottery
                                                    {
                                                        Name = g.Key.Name,
                                                        CreatedBy = g.Key.CreatedBy,
                                                        ModifiedOn = g.Key.ModifiedOn,
                                                        Id = g.Key.Id
                                                    });
    }
    

    我也觉得你对viewModel的理解是对的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多