如何在C#中使用Except

时间:2012-12-18 14:23:42

标签: c# linq

我有以下内容为我提供了我想要的2个数据表中的字段相等

var matched = from s in sendTable.AsEnumerable()
                     join r in recvTable.AsEnumerable() on
                     new { BUS = s.Field<int>("BUS"), 
                           IDENT = s.Field<int>("IDENT"), 
                           STATION = s.Field<int>("STATION"), 
                           REF1 = s.Field<string>("REF1"), 
                           REF2 = s.Field<string>("REF2"),
                           REF3 = s.Field<string>("REF3"),
                           REF4 = s.Field<string>("REF4"),
                           REF5 = s.Field<string>("REF5"),
                           REF6 = s.Field<string>("REF6"),
                           REF7 = s.Field<string>("REF7"),
                           REF8 = s.Field<string>("REF8") } equals
                      new { BUS = r.Field<int>("BUS"), 
                           IDENT = r.Field<int>("IDENT"), 
                           STATION = r.Field<int>("STATION"), 
                           REF1 = r.Field<string>("REF1"), 
                           REF2 = r.Field<string>("REF2"),
                           REF3 = r.Field<string>("REF3"),
                           REF4 = r.Field<string>("REF4"),
                           REF5 = r.Field<string>("REF5"),
                           REF6 = r.Field<string>("REF6"),
                           REF7 = r.Field<string>("REF7"),
                           REF8 = r.Field<string>("REF8") } 
                     select new { Send = s, Receive = r };

我现在想要的是不平等如何使用Except进行此操作?

1 个答案:

答案 0 :(得分:1)

您可以在这些字段中使用Enumerable.Except

var sendIdentityFields = from s in sendTable.AsEnumerable()
                         select new
                         {
                             BUS = s.Field<int>("BUS"),
                             IDENT = s.Field<int>("IDENT"),
                             STATION = s.Field<int>("STATION"),
                             REF1 = s.Field<string>("REF1"),
                             REF2 = s.Field<string>("REF2"),
                             REF3 = s.Field<string>("REF3"),
                             REF4 = s.Field<string>("REF4"),
                             REF5 = s.Field<string>("REF5"),
                             REF6 = s.Field<string>("REF6"),
                             REF7 = s.Field<string>("REF7"),
                             REF8 = s.Field<string>("REF8")
                         };
var receivedIdentityFields = from r in sendTable.AsEnumerable()
                         select new
                         {
                             BUS = r.Field<int>("BUS"),
                             IDENT = r.Field<int>("IDENT"),
                             STATION = r.Field<int>("STATION"),
                             REF1 = r.Field<string>("REF1"),
                             REF2 = r.Field<string>("REF2"),
                             REF3 = r.Field<string>("REF3"),
                             REF4 = r.Field<string>("REF4"),
                             REF5 = r.Field<string>("REF5"),
                             REF6 = r.Field<string>("REF6"),
                             REF7 = r.Field<string>("REF7"),
                             REF8 = r.Field<string>("REF8")
                         };
var sendButNotReceived = sendIdentityFields.Except(receivedIdentityFields);

当然,这只选择包含这些字段的匿名类型而不是DataRows。如果您需要选择DataRows sendTable,则需要在相同字段中将此结果加入其中。