簡體   English   中英

使用 LINQ 將值列表與對象(列表)的屬性進行比較

[英]Comparing a list of values against the property of an object (list) using LINQ

我在使用 Linq 比較兩個列表時遇到問題。
我的第一個列表包含對象實例,其中包含感興趣的屬性,要與僅包含值的第二個列表進行比較。
假設我們有:

List<uint> referenceValues = new List<uint>(){1, 2, 3};
and
List<SomeObject> myObject= new List<SomeObject>();

現在 SomeObject() 類具有 uint 類型的屬性“Value”。
請注意,“myObject”列表可能包含比“referenceValues”列表更多的元素,因此我們必須選擇“myObject”的適當部分與“referenceValues”進行比較。

我發現了以下堆棧溢出問題,這是相關的,但不是我所追求的: 鏈接

這是我迄今為止嘗試過的,但沒有成功:

if (myObject
    .GetRange((int)offset, count)       // Extracted the object of interest from the list
    .Where(x => referenceValues.Equals(x.Value)).Count() == 0)   // Comparing the "Value" properties against the "referenceValues"
{
    // Do something
}

我想我需要以某種方式使用“SequenceEqual”,但我不知道如何使用。
任何建議將非常受歡迎。

編輯

可重現的例子:

using System;
using System.Linq;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        int offset = 0;
            
        List<uint> referenceValues = new List<uint>(){1, 2, 3};

        List<SomeObject> myObject= new List<SomeObject>(){new SomeObject(){Value=1}, 
                                                          new SomeObject(){Value=2},
                                                          new SomeObject(){Value=3}};
        
        if (myObject
            .GetRange(offset, referenceValues.Count()-1)       // Extracted the object of interest from the list
            .Where(x => referenceValues.Equals(x.Value)).Count() == referenceValues.Count())   // Comparing the "Value" properties against the "referenceValues"
        {
            // Do something
            Console.WriteLine("Lists are matching!");
        }
        else
        {
            Console.WriteLine("Lists are not matching!");
        }
    }
}

public class SomeObject
{
    public uint Value = 0;
}

編輯2

根據 Guru Stron 的建議工作的解決方案:

using System;
using System.Linq;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        int offset = 0;
            
        List<uint> referenceValues = new List<uint>(){1, 2, 3};

        List<SomeObject> myObject= new List<SomeObject>(){new SomeObject(){Value=1}, 
                                                          new SomeObject(){Value=2},
                                                          new SomeObject(){Value=3}};
        
        if (myObject.
            GetRange((int)offset, referenceValues.Count())   
            .Select(someObject => someObject.Value)
            .SequenceEqual(referenceValues))
        {
            // Do something
            Console.WriteLine("Lists are matching!");
        }
        else
        {
            Console.WriteLine("Lists are not matching!");
        }
    }
}

public class SomeObject
{
    public uint Value = 0;
}

關於 Linq 表達式的推薦書:LINQ Pocket Reference

如果您想檢查myObject值的某些子集是否按某些屬性具有特定順序,您可以執行以下操作:

bool inOrder = myObject
    .GetRange((int)offset, count)   
    .Select(someObject => someObject.Value)
    .SequenceEqual(referenceValues);

如果您只想檢查指定的referenceValues是否存在所有值,您可以使用All (如果源范圍為空,則返回true ):

bool allPresent = myObject
    .GetRange((int)offset, count)   
    .All(someObject => referenceValues.Contains(someObject.Value));

對於沒有訂購的完整系列匹配,我不知道開箱即用的解決方案,但您可以在這里尋找一個示例。

if(myObject.Any(x => referenceValues.Contains(x.Value)) {}

我忽略了 GetRange 部分,但應該沒有區別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM