简体   繁体   English

如何使用Linq在列上使用Distinct

[英]How to use a Distinct on a column using Linq

Here is my code: 这是我的代码:

var query = from row1 in table.AsEnumerable()
                         let time = row1.Field<DateTime>("time")
                         let uri = row1.Field<string>("cs-uri-stem")
                         let ip = row1.Field<string>("c-ip")
                         let questionid = row1.Field<int>("questionid")
                         where questionid == int.Parse(table.Rows[x]["questionid"].ToString())
                         select new
                         {
                             time,
                             uri,
                             ip,
                             questionid
                         };

The ip column should be unique. ip列应该是唯一的。 I can't have duplicate items in the ip field. 我不能在ip字段中有重复的项目。 is it possible to do this in linq 是否可以在linq中执行此操作

You can achieve what you want by grouping by the ip address, but then you'll need to know how you want to handle the other fields when you do have duplicates. 您可以通过ip地址分组来实现您想要的功能,但是当您有重复项时,您需要知道如何处理其他字段。

var query = from row1 in table.AsEnumerable()
                     let time = row1.Field<DateTime>("time")
                     let uri = row1.Field<string>("cs-uri-stem")
                     let ip = row1.Field<string>("c-ip")
                     let questionid = row1.Field<int>("questionid")
                     where questionid == int.Parse(table.Rows[x]["questionid"].ToString())
                     group by ip into g
                     select new
                     {
                         time = g.time.First(),
                         uri = g.uri.First(),
                         ip = g.Key,
                         questionid = g.questionid.First()
                     };

You can only perform a Distinct on all the fields you select, not just on one field (which values would you take for other fields ?). 您只能对所选的所有字段执行“ Distinct ”,而不只是在一个字段上执行(对于其他字段,您将采用哪些值?)。

You can achieve that by using the Distinct extension method : 您可以使用Distinct扩展方法实现此目的:

        var query = (from row1 in table.AsEnumerable()
                     let time = row1.Field<DateTime>("time")
                     let uri = row1.Field<string>("cs-uri-stem")
                     let ip = row1.Field<string>("c-ip")
                     let questionid = row1.Field<int>("questionid")
                     where questionid == int.Parse(table.Rows[x]["questionid"].ToString())
                     select new
                     {
                         time,
                         uri,
                         ip,
                         questionid
                     }).Distinct();

You can also use Distinct() with an equality comparer (ie, pass in a method that will compare the IP value, and ignore the rest). 您还可以将Distinct()与等式比较器一起使用(即,传入将比较IP值的方法,并忽略其余的)。 But as tvanfosson asks, what is the correct procedure for handling duplicate values? 但正如tvanfosson所问,处理重复值的正确程序是什么?

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

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