是否有 LINQ 或 C# 方法可以幫助使用 lambda 和 LINQ 在 C# 中測驗兩個范圍內的元素是否相等。
事實上,我有 2 個物件陣列,我想檢查兩個陣列物件的屬性是否相同。
在 C 中,有可以與 lambda 一起使用的 std::equal (示例來自https://www.cplusplus.com/reference/algorithm/equal/)
// equal algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::equal
#include <vector> // std::vector
bool mypredicate (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100
std::vector<int>myvector (myints,myints 5); // myvector: 20 40 60 80 100
myvector[3]=81; // myvector: 20 40 60 81 100
// using predicate comparison:
if ( std::equal (myvector.begin(), myvector.end(), myints, [](int i, int j){ return i % 2 == 0 && j % 2 == 0; }) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
return 0;
}
更新:另一個接近我想要做的 C 示例:
#include <algorithm> // std::equal
#include <iostream> // std::cout
#include <string> // std::string
#include <vector> // std::vector
using namespace std;
struct Device
{
Device(string id) : id(id) {}
string id;
};
int main () {
Device mydevices[] = {Device("A1"), Device("A2")};
std::vector<Device> myvector(mydevices,mydevices 2);
mydevices[1].id = "B1";
// using predicate comparison:
if ( std::equal (myvector.begin(), myvector.end(), mydevices, [](Device i, Device j){ return i.id == j.id; }) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
return 0;
}
uj5u.com熱心網友回復:
您可以使用SequenceEqual:
int[] myints = {20,40,60,20,40};
IEnumerable<int> range1 = myints.Take(2);
IEnumerable<int> range2 = myints.Skip(3);
bool equalRanges = range1.SequenceEqual(range2); // true
如果您需要比較物件屬性,您有多種選擇:
覆寫
Equals并GetHashCode使用上面的代碼實作
IEquatable<YourObjectType>和使用上面的代碼實作一個自定義
IEqualityComparer<YourObjectType>并使用它的一個實體作為引數SequenceEqual和上面的代碼使用不同的 LINQ 查詢,如下所示:
YourObjectType[] myobjects = {...}; IEnumerable<YourObjectType> range1 = myobjects.Take(2); IEnumerable<YourObjectType> range2 = myobjects.Skip(3); bool equalRanges = range1.Zip(range2, (x1, x2) => AreEqual(x1, x2)).All(b => b);
(AreEqual比較屬性的方法在哪里,您也可以行內執行此操作)
uj5u.com熱心網友回復:
您可以使用ZipIEnumerable 來創建元組。您All可以檢查所有這些元組是否滿足條件:
Device[] devices1 = {new Device("A1"), new Device("A2")};
Device[] devices2 = {new Device("A1"), new Device("A2")};
if(devices1.Zip(devices2).All(x => x.Item1.ID == x.Item2.ID))
{
Console.WriteLine("devices1 and devices2 IDs are the same");
}
在線演示:https ://dotnetfiddle.net/FIFOYP
uj5u.com熱心網友回復:
解決您編輯的問題:
看來您有一個型別的陣列,該陣列具有要比較的屬性,而忽略了其他屬性。
此外,您要比較的屬性的型別實作了IEquals<T>and GetHashCode()(因為它是 a string)。
在這種情況下,您可以像這樣比較兩個陣列:
using System;
using System.Linq;
static class Program
{
public static void Main()
{
TestClass[] a1 = { new ("1", 1), new ("2", 2), new ("3", 3) };
TestClass[] a2 = { new ("1", 2), new ("2", 3), new ("3", 4) };
bool propertiesAreEqual =
a1.Select(element => element.PropertyToCompare)
.SequenceEqual(
a2.Select(element => element.PropertyToCompare));
Console.WriteLine(propertiesAreEqual); // True
}
}
record TestClass(string PropertyToCompare, int SomeOtherProperty);
在 DotNetFiddle 中嘗試:https ://dotnetfiddle.net/NR6bog
注意:我只是record為了簡潔起見。當然,這也適用于普通課程。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/431543.html
上一篇:如何從嵌套物件串列中洗掉重復項?
