简体   繁体   English

按LINQ实体框架分组

[英]Group by LINQ Entity Framework

I am having trouble grouping a data retrieved from database which has duplication due to a column with data i don't need. 我在对从数据库中检索到的数据进行分组时遇到麻烦,由于有不需要的列,该数据库具有重复性。 i was trying to use distinct or group by to get rid of the redundancies. 我正在尝试使用“区分”或“分组依据”来消除重复。

var students= db.student.Distinct().Where(c => c.classdate== tomorrow || c.classdate== todayd).ToList();

it will return student's name but also duplicates student record according to course available on that day. 它会返回学生的姓名,但也会根据当天的课程复制学生记录。 I just wanted to get list of students who has class today and tomorrow. 我只想获取今天和明天上课的学生名单。

What I have tried: 我试过的

students.GroupBy(x => new { x.Fn  ame, x.classdate});

The above seems to return result set with only the group by columns and 上面的似乎返回的结果集只有group by列和

@model IEnumerable

does not seem to be compatible with the return type. 似乎与返回类型不兼容。

If you want the first student of your group, you can use this LINQ query: 如果要组中的第一个学生,可以使用以下LINQ查询:

students.GroupBy(x => new { x.Fname, x.classdate}).Select(grp => grp.First);

This will yield a generic IEnumerable<T> where T is your stident type. 这将产生一个通用的IEnumerable<T> ,其中T是您的静态类型。

From what I understand, you want to get all the student names who have classes either today or tomorrow. 据我了解,您想获得今天或明天上课的所有学生姓名。 Your LINQ expression is not accurate if that is the case. 如果是这样,您的LINQ表达式将不正确。 Your expression should do filter, select and distinct operations in that order. 您的表达式应按该顺序过滤,选择和区分操作。 Something like this: 像这样:

db.student.Where(<condition here>).Select(<columns to select here>).Distinct(<comparer here; optional>)

for "distinct by" you can grouping, and pick the fist row from each group. 对于“与众不同”,您可以分组,然后从每个组中选择第一排。

so in model: 所以在模型中:

return students.GroupBy(x => new { x.Fname, x.classdate}).Select(x => x.First()).ToList();

in view: 鉴于:

@model IEnumerable<student>

Just select the column from student table as you want and use Distinct(), like wise your query should be 只需从学生表中选择所需的列,然后使用Distinct(),就像明智的选择

var students = (from objSql in db.student.Where(t => t.classdate == tomorrow || t.classdate == todayd) select new CustomObject() { StudentName = objSql.StudentName }).Distinct().ToList(); var students =(从db.student.Where(t => t.classdate ==明天|| t.classdate ==今天)中的objSql选择新的CustomObject(){StudentName = objSql.StudentName})。Distinct()。ToList ();

I think you should like this. 我想你应该这样。

context.Student.Select(i => new { i.Fn, i.classdate }).Where(c => c.classdate == tomorrow || c.classdate == todayd).Distinct();

If you run this code, it generate following query in Database. 如果运行此代码,它将在数据库中生成以下查询。

exec sp_executesql N'SELECT 
    [Distinct1].[C1] AS [C1], 
    [Distinct1].[Fn] AS [Fn], 
    [Distinct1].[classdate] AS [classdate]
    FROM ( SELECT DISTINCT 
        [Extent1].[Fn] AS [Fn], 
        [Extent1].[classdate] AS [classdate], 
        1 AS [C1]
        FROM [dbo].[Student] AS [Extent1]
        WHERE [Extent1].[classdate] IN (@p__linq__0,@p__linq__1)
    )  AS [Distinct1]',N'@p__linq__0 datetime2(7),@p__linq__1 datetime2(7)',@p__linq__0='2018-04-17 00:00:00',@p__linq__1='2018-04-16 00:00:00'

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

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