区分数据表中的多个列

时间:2016-05-11 06:32:25

标签: c# sql asp.net linq

尝试执行SQL查询(<plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${tomcat_home}/webapps</outputDirectory> <resources> <resource> <directory>${project.build.directory}/eBuddy</directory> </resource> </resources> </configuration> </execution> </executions> )会执行的操作,但我使用LINQ和数据表执行此操作。

修改

SQL应该看起来像一个Mysql

SELECT DISTINCT (first,second),third FROM table

select first, second, third
 FROM table
 group by first, second

此代码正在执行的操作是,DataTable secondTable = new DataTable(); secondTable.Columns.Add("name", typeof(string)); secondTable.Columns.Add("date", typeof(string)); secondTable.Columns.Add("clockIn", typeof(string)); secondTable.Columns.Add("clockOut", typeof(string)); var t4 = (from a in firstTable.AsEnumerable() select new { name = a.Field<string>("name"), date = a.Field<string>("date"), clockIn = a.Field<string>("clockIn"), clockOut = a.Field<string>("clockOut") }).Distinct(); var t5 = (from a in firstTable.AsEnumerable() select new { name = a.Field<string>("name"), date = a.Field<string>("date") }).Distinct(); var t6 = (from d in t5 join a in t4 on new { d.name, d.date } equals new { a.name, a.date } select secondTable.LoadDataRow( new object[] { d.name,d.date,a.clockIn,a.clockOut }, false)).ToList(); ViewBag.Data = secondTable; 加入t4t5,但没有排除。虽然我想要的是t6t4中的所有行都应该根据(姓名,日期)和来自t5的所有行加入t6应排除t5中存在的#39; t。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

根据您的评论,您可以按所需字段进行分组,并获取任何分组结果。

您可以通过clockin或clockout订购以获得更少的&#34;随机&#34;结果

var t6 = firstTable.AsEnumerable()
                   .GroupBy(a => new {
                                     name = a.Field<string>("name"),
                                     date = a.Field<string>("date")
                                     }
                            )
                   .Select(g => g.First())
                   //or Select(g => g.OrderBy(a => a.Field<string>("clockIn")).First()
                   .ToList();