两个如何比较linq查询vb.net中的两个数组列表项

时间:2016-09-07 17:27:18

标签: vb.net linq

我有两个查询(qry1返回[a1],[a2]; qry2返回[a1],[a2],[a3])。所以我想比较这两个查询。如果它们不相等则执行某些功能。以下是我正在尝试的查询。我不想与' count'操作

Dim Qry1 = (From x In db1.Approvals _
        Where x.ID = tId _
        And x.APPROVALID = GetRqstID(thisId) _
        Select x.APPROVERID).ToList()


 Dim Qry2 = (From x In db.Resources _
             Join y In db.Users On x.USER_ID Equals y.USER_ID _
             Where x.ID = tskIdIn _
             And x.TYPE = rsrcType _
             Order By y.FIRST_NAME _
            Select x.USER_ID).ToList()


 If ((Qry1.ToArray) <> (Qry2.ToArray)) Then
---
---
 End If

2 个答案:

答案 0 :(得分:0)

Linq Intersect将是最佳人选。

int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };

IEnumerable<int> both = id1.Intersect(id2);

foreach (int id in both)
    Console.WriteLine(id);

/*
 This code produces the following output and means you have some element repeating on both array instance. If there is no matching element then you can perform the operation that you wanted.:

 26
 30
*/

答案 1 :(得分:0)

if (Qry1.Count() != Qry2.Count() || 
    Qry1.Except(Qry2).Count() > 0 ||
    Qry2.Except(Qry1).Count() > 0)  
 // not the same.

我首先进行计数检查,因为短路会加速大多数情况。如果你正在做其他两个,则不需要它。

https://msdn.microsoft.com/en-us/library/bb300779%28v=vs.110%29.aspx