Linq到SQL /筛选器重复 [英] Linq to SQL / filter duplicates

查看:169
本文介绍了Linq到SQL /筛选器重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在我的SQL Server 2012中有一个重复的几个视图,我想排序他们的最新和筛选所有其他人 - 任何人都可以帮我吗?

我的视图在我的SQL Server 2012中:
$ b $ p GUID(作为主键),数字,日期时间和名称

  + ----- + ---------- + ----------------------- --------- + ----- 
| guid |号码| datetime |名称
+ ----- + ---------- + --------------------------- ----- + ------
| B105 .. | 1234567 | 2014-07-07T16:32:20.854 + 02:00:00 | Name1
| s1b5 .. | 1111222 | 2014-07-06T16:30:21.854 + 02:00:00 | Name2
| b17a .. | 1234567 | 2014-07-06T15:22:17.854 + 02:00:00 | Name1
| F205 .. | 1233333 | 2014-07-07T17:40:20.854 + 02:00:00 | Name3
| b11t .. | 1233333 | 2014-07-04T11:12:15.854 + 02:00:00 | Name3
| rt85 .. | 1111222 | 2014-07-07T21:55:52.854 + 02:00:00 | Name2
+ ------- + -------- + --------- ----------------------- + -----

这个名字每次都是一样的,如果这个数字是一样的。例如,我想过滤我的表,我只有最新的数字没有重复





所以结果应该是:

pre $ + ----- + ---------- + -------------------------------- + -----
| guid |号码| datetime |名称
+ ----- + ---------- + --------------------------- ----- + ------
| B105 .. | 1234567 | 2014-07-07T16:32:20.854 + 02:00:00 | Name1
| F205 .. | 1233333 | 2014-07-07T17:40:20.854 + 02:00:00 | Name3
| rt85 .. | 1111222 | 2014-07-07T21:55:52.854 + 02:00:00 | Name2
+ ------- + -------- + --------- ----------------------- + -----

我如何在Linq中做到这一点?由于guid和datetime,Distinct不工作。

解决方案

  var res = list.GroupBy(c => c.name).Select(group => group.OrderBy(c1 => c1.datetime).First())。 

只要将datetime存储为 DateTime ,而不是字符串。


i have a view in my sql server 2012 with a couple of duplicates and i want to sort them by the newest and filter all others - can anyone help me?

My viewin my SQL Server 2012:

GUID (as primary key), number, datetime and name

+-----+----------+--------------------------------+-----
| guid  |  number| datetime                       | name
+-----+----------+--------------------------------+------
| b105..| 1234567|2014-07-07T16:32:20.854+02:00:00|Name1
| s1b5..| 1111222|2014-07-06T16:30:21.854+02:00:00|Name2
| b17a..| 1234567|2014-07-06T15:22:17.854+02:00:00|Name1
| f205..| 1233333|2014-07-07T17:40:20.854+02:00:00|Name3
| b11t..| 1233333|2014-07-04T11:12:15.854+02:00:00|Name3
| rt85..| 1111222|2014-07-07T21:55:52.854+02:00:00|Name2
+-------+--------+--------------------------------+-----

the name is every time the same if the number is the same. for e.g. number 1234567 is always name 1.

I want to filter my table that i have only the newest number without duplicates

so the result should be:

+-----+----------+--------------------------------+-----
| guid  |  number| datetime                       | name
+-----+----------+--------------------------------+------
| b105..| 1234567|2014-07-07T16:32:20.854+02:00:00|Name1
| f205..| 1233333|2014-07-07T17:40:20.854+02:00:00|Name3
| rt85..| 1111222|2014-07-07T21:55:52.854+02:00:00|Name2
+-------+--------+--------------------------------+-----

How can i do this in Linq? "Distinct" is not working because of the guid and the datetime

解决方案

var res = list.GroupBy(c => c.name).Select(group => group.OrderBy( c1 => c1.datetime).First()).ToList();

This should work as long as datetime is stored as an instance of DateTime, instead of string.

这篇关于Linq到SQL /筛选器重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆