【问题标题】:How two compare two array list items in linq query vb.net如何在 linq 查询 vb.net 中比较两个数组列表项
【发布时间】:2016-09-07 17:27:18
【问题描述】:

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

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

【问题讨论】:

  • 尝试查找 UNION JOIN INTERSECT。您已在 vb.net 问题中添加了一个 c# 标签——您可能希望删除该标签

标签: vb.net linq


【解决方案1】:
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

【讨论】:

  • 我不想使用 count 我想比较值并检查它们是否相等
  • @Adu - 请阅读除此之外的内容。你不明白我的回答它做你想要的。这就是我发布它的原因。
【解决方案2】:

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
*/

【讨论】:

  • 实际上,我想检查两个查询是否相等,而不是计数值示例:q1(a1,a2);q2(a2,a3) 即使计数值相同不一样
  • 我们不是在谈论元素的数量,而是元素的可用性。
猜你喜欢
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多