【发布时间】: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