C# 如何按Linq语句分组

C# 如何按Linq语句分组,c#,linq,C#,Linq,如何根据此linq语句进行分组 public IQueryable<Lottery> GetLotteriesByLotteryOfferId(int lotteryOfferId) { return this.db.LotteryOffers .Where(lo => lo.Id == lotteryOfferId) .SelectMany(

如何根据此linq语句进行分组

public IQueryable<Lottery> GetLotteriesByLotteryOfferId(int lotteryOfferId)
{
    return this.db.LotteryOffers
                                .Where(lo => lo.Id == lotteryOfferId)
                                .SelectMany(lo => lo.LotteryDrawDates)
                                .Select(ldd => ldd.Lottery);                                        
}
public IQueryable GetLotteriesByLotteryOfferId(int-lotteryOfferId)
{
返回此.db.LotteryOffers
.Where(lo=>lo.Id==lotteryOfferId)
.SelectMany(lo=>Lot.LotteryDrawDates)
.Select(ldd=>ldd.lotking);
}
这不起作用:

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
                                                });
}
public IQueryable GetLotteriesByLotteryOfferId(int-lotteryOfferId)
{
返回此.db.LotteryOffers
.Where(lo=>lo.Id==lotteryOfferId)
.SelectMany(lo=>Lot.LotteryDrawDates)
.Select(ldd=>ldd.lotking)
.GroupBy(s=>new{s.Name,s.CreatedBy,s.ModifiedOn,s.Id})
.选择(g=>新彩票
{
Name=g.Key.Name,
CreatedBy=g.Key.CreatedBy,
ModifiedOn=g.Key.ModifiedOn,
Id=g.Key.Id
});
}
我得到的错误:

无法在LINQ中构造实体或复杂类型“彩票” 对实体进行查询


我使用数据服务(web服务)。

LINQ to SQL和LINQ to Entities-不同于LINQ to Object和LINQ to Xml-使用更高阶的方法生成将在数据库上运行的SQL,因此lambda没有该语言的全部功能。在
选择
“子句”中创建新的
彩票
毫无意义-您不想在数据库进程中创建那些
彩票
对象,而是想在应用程序的进程中创建它们

您需要做的是使用:


AsEnumerable
生成SQL之前的所有内容。调用
AsEnumerable
会强制C#执行查询并将结果作为一个对象流来获取-这是
Select
现在可以用来生成
lotking
对象的东西。

问题还是错误???这里的
groupby
的目的是什么?我的观点是,我想在分组时返回行数,但是,我必须使用匿名类型,在这种情况下,我不能使用,因为我使用数据服务(Web服务),我需要返回类型彩票。在这种情况下,您可能需要考虑使用而不是<代码> GROPBB< <代码>。您想得到什么?特定彩票的总报价数?我是否使用了Distinct(),但如何创建自定义“字段”,以便在视图中显示计数?阿比吉特·帕特尔说得对。阿比吉特·帕特尔,你知道我需要什么:P我一整天都在用这个碰壁。
 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
                                            });