简体   繁体   English

LINQ to Entities Group By,然后选择最小值

[英]LINQ to Entities Group By and then select the min value

I'm using VS2012 and SQL Compact Edition and I have difficult time to create my database. 我正在使用VS2012和SQL Compact Edition,并且我很难创建数据库。 I'm trying to use the designer from VS2012 but I still can't do simple things like defining Foreign keys and stuff like that, but I need to prepare a Demo fast so instead I decided to leave the learning for later and for now I work with only one table in my database (it's a small project) and I have less than 500 rows so it's not that big of a problem. 我正在尝试使用VS2012中的设计器,但是我仍然无法做一些简单的事情,例如定义外键和类似的东西,但是我需要快速准备一个演示,所以我决定将学习留给以后,现在我我的数据库中只有一个表(这是一个小项目),并且我的行数少于500,所以这不是一个大问题。 However I need to do this : 但是我需要这样做:

I have insurance policies everyone of which has unique Policy number. 我有每个人都有唯一保单号的保险单。 But for one Policy there might be several dates when the customer need to make payments. 但是对于一项保单,客户可能需要数个付款日期。 So based on the information above in my table I have say four rows with different IDs but with the same Policy number and different date for each record. 因此,根据我表中的上述信息,我说四行具有不同的ID,但每条记录具有相同的策略编号和不同的日期。

I use MDI Windows Forms and I have a form where I show info for all clients and here comes the problem. 我使用MDI Windows窗体,并且有一个窗体,其中显示了所有客户端的信息,并且出现了问题。 If one policy has several dates or in other words several records in my table I want to show only the record with the upcoming date, and only if the user clicks "details" to show all date for this insurance policy. 如果一个保单有多个日期,或者换句话说,我的表中有多个记录,我只想显示带有即将到来日期的记录,并且仅当用户单击“详细信息”以显示此保险单的所有日期时。

So what I need is to query the table group the results by PolicyNumber and for each policy that has several dates to take the record with the most recent date. 因此,我需要按PolicyNumber和具有多个日期的每个策略查询表组的结果,以获取具有最新日期的记录。

For now I have this: 现在我有这个:

 using (RalBaseEntities ctx = new RalBaseEntities())
            {
                var mainGridInfo = from cs in ctx.MainInfo
                                   where cs.Id != null
                                   select cs;

                IList<MainInfo> mainGridInfoList = mainGridInfo.ToList<MainInfo>();
                dgvClients.DataSource = mainGridInfoList;
            }

So having this I try to use group by like this: 因此,有了这个,我尝试像这样使用group by

var mainGridInfo = from cs in ctx.MainInfo
                                   where cs.Id != null
                                   group cs by cs.PolicyNumber into test
                                   select test;

But then I get this marked as error : IList mainGridInfoList = mainGridInfo.ToList<MainInfo>(); 但是然后我将其标记为错误:IList mainGridInfoList = mainGridInfo.ToList<MainInfo>();

And the error is : 错误是:

Error   1   'System.Linq.IQueryable<System.Linq.IGrouping<string,SHAdmin.MainInfo>>' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.ParallelEnumerable.ToList<TSource>(System.Linq.ParallelQuery<TSource>)' has some invalid arguments  C:\Users\X-Man\Desktop\SHAdmin\SHAdmin\Forms\Clients.cs 44  52  SHAdmin

And even if this worker I still don't know how to extract only the record with the most recent date. 即使这个工作人员,我仍然不知道如何仅提取最近日期的记录。

Your query: 您的查询:

var mainGridInfo = from cs in ctx.MainInfo
                   where cs.Id != null
                   group cs by cs.PolicyNumber into test
                   select test;

returns IQueryable<IGrouping<string, MainInfo>> , so you can't call ToList<MainInfo> on it! 返回IQueryable<IGrouping<string, MainInfo>> ,因此您无法对其调用ToList<MainInfo>

According to your requirements: 根据您的要求:

So what I need is to query the table group the results by PolicyNumber and for each policy that has several dates to take the record with the most recent date. 因此,我需要按PolicyNumber和具有多个日期的每个策略查询表组的结果,以获取具有最新日期的记录。

I think you're looking for: 我认为您正在寻找:

var mainGridInfo = from cs in ctx.MainInfo
                   where cs.Id != null
                   group cs by cs.PolicyNumber into test
                   select test.OrderByDescending(cs => cs.Date).First();

And it will return IQueryable<MainInfo> , so you should be able to call ToList<MainInfo> on it. 并且它将返回IQueryable<MainInfo> ,因此您应该能够在其上调用ToList<MainInfo>

Try something like 尝试类似

var mainGridInfo = from cs in ctx.MainInfo
                                   where cs.Id != null
                                   group cs by cs.PolicyNumber into test
                                   select new { Key = test.Key, MainInfo = test};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM