0

我的第一个问题在这里: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 to Entities 查询中构造实体或复杂类型“彩票”。

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

附加问题 如果我想使用自定义视图模型,我只需在客户端创建它并选择“viewModel”类型而不是带有选择的彩票?

例子:

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

2 回答 2

1

I think that the error is that you use the class Lottery in your selector. LINQ to entity can only construct pur "Data Transfert Object" : class containing only public properties with trivial getter and setter and without constructor.

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,
                               ...    
                            });
于 2014-01-16T22:57:14.820 回答
0

我认为错误是 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 的理解是正确的。

于 2013-10-20T18:54:47.563 回答