如何在linq中比较两个不区分大小写的复杂对象
how to compare two complex object without case sensitive in linq
我必须列出对象。所以我需要比较这些对象并从 "datActualItem" 到列表中获得满意的列表。列表 "datActualItem" 项可能区分大小写,但列表 "datFiltItem" 项都是小写字母我的代码如下。
var datActualItem = (List<UserRoleListViewModel>)TempResult.ToList();
var datFiltItem = ((List<UserRoleListViewModel>)usersDataSource.Data).ToList();
var objnewm = new List<UserRoleListViewModel>();
foreach (var item in datActualItem)
{
objnewm.Add(datActualItem.Where(s => s.Equals(datFiltItem)).FirstOrDefault());
}
Note:- The array list item Firstname is "Sajith" other list is containing "sajith" so currently not checking due to this.
I need to checking without case sensitive and to
the add list from the "datActualItem"
使用StringComparison.OrdinalIgnoreCase
bool equals = string.Equals("Test", "test", StringComparison.OrdinalIgnoreCase);
如果您需要在 Except
方法中使用它,您应该创建实现 IEqualityComparer
(参见 this page)的 class 并在中使用 string.Equals("Test", "test", StringComparison.OrdinalIgnoreCase)
class 比较两个复杂的对象。因为 Linq 不知道你在比较什么数据。
默认情况下它会检查 GetHashCode
和 Equals
的结果(为任何对象实现)并且引用类型的默认实现是在引用本身上。
要使用自定义比较策略比较 2 个列表,您可以创建一个实现 IEqualityComparer<T>
:
的 class
public class MyClassComparer : IEqualityComparer<UserRoleListViewModel>
{
public bool Equals(UserRoleListViewModel x, UserRoleListViewModel y)
{
return x.ID == y.ID
&& x.FirstName.Equals(y.FirstName, StringComparison.CurrentCultureIgnoreCase)
&& x.LastName.Equals(y.LastName, StringComparison.CurrentCultureIgnoreCase);
// continue to add all the properties needed in comparison
}
public int GetHashCode(MyClass obj)
{
StringComparer comparer = StringComparer.CurrentCultureIgnoreCase;
int hash = 17;
hash = hash * 31 + obj.ID.GetHashCode();
hash = hash * 31 + (obj.FirstName == null ? 0 : comparer.GetHashCode(obj.FirstName));
hash = hash * 31 + (obj.LastName == null ? 0 : comparer.GetHashCode(obj.LastName));
// continue all fields
return hash;
}
}
用法:
var list = actual.Except(expected, new MyClassComparer());
另一种方法是覆盖您自己的 class UserRoleListViewModel
的相等性,但这会影响一切,而不仅仅是这个 Except
方法。
我必须列出对象。所以我需要比较这些对象并从 "datActualItem" 到列表中获得满意的列表。列表 "datActualItem" 项可能区分大小写,但列表 "datFiltItem" 项都是小写字母我的代码如下。
var datActualItem = (List<UserRoleListViewModel>)TempResult.ToList();
var datFiltItem = ((List<UserRoleListViewModel>)usersDataSource.Data).ToList();
var objnewm = new List<UserRoleListViewModel>();
foreach (var item in datActualItem)
{
objnewm.Add(datActualItem.Where(s => s.Equals(datFiltItem)).FirstOrDefault());
}
Note:- The array list item Firstname is "Sajith" other list is containing "sajith" so currently not checking due to this. I need to checking without case sensitive and to the add list from the "datActualItem"
使用StringComparison.OrdinalIgnoreCase
bool equals = string.Equals("Test", "test", StringComparison.OrdinalIgnoreCase);
如果您需要在 Except
方法中使用它,您应该创建实现 IEqualityComparer
(参见 this page)的 class 并在中使用 string.Equals("Test", "test", StringComparison.OrdinalIgnoreCase)
class 比较两个复杂的对象。因为 Linq 不知道你在比较什么数据。
默认情况下它会检查 GetHashCode
和 Equals
的结果(为任何对象实现)并且引用类型的默认实现是在引用本身上。
要使用自定义比较策略比较 2 个列表,您可以创建一个实现 IEqualityComparer<T>
:
public class MyClassComparer : IEqualityComparer<UserRoleListViewModel>
{
public bool Equals(UserRoleListViewModel x, UserRoleListViewModel y)
{
return x.ID == y.ID
&& x.FirstName.Equals(y.FirstName, StringComparison.CurrentCultureIgnoreCase)
&& x.LastName.Equals(y.LastName, StringComparison.CurrentCultureIgnoreCase);
// continue to add all the properties needed in comparison
}
public int GetHashCode(MyClass obj)
{
StringComparer comparer = StringComparer.CurrentCultureIgnoreCase;
int hash = 17;
hash = hash * 31 + obj.ID.GetHashCode();
hash = hash * 31 + (obj.FirstName == null ? 0 : comparer.GetHashCode(obj.FirstName));
hash = hash * 31 + (obj.LastName == null ? 0 : comparer.GetHashCode(obj.LastName));
// continue all fields
return hash;
}
}
用法:
var list = actual.Except(expected, new MyClassComparer());
另一种方法是覆盖您自己的 class UserRoleListViewModel
的相等性,但这会影响一切,而不仅仅是这个 Except
方法。