[英]how to compare two complex object without case sensitive in linq
I have to list object. 我必须列出对象。 so i need to compare these object and get satisfied list from "datActualItem" to the list.
所以我需要比较这些对象,并从“datActualItem”到列表获得满意的列表。 The list "datActualItem" items may be case sensitive but the list "datFiltItem" items all are small letter my code below.
列表“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.
注意: - 数组列表项Firstname是“Sajith”,其他列表包含“sajith”,因此目前没有检查。 I need to checking without case sensitive and to the add list from the "datActualItem"
我需要在不区分大小写的情况下检查并从“datActualItem”添加列表
To compare 2 lists with custom comparison strategy, you can create a class that implements IEqualityComparer<T>
: 要将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;
}
}
Usage: 用法:
var list = actual.Except(expected, new MyClassComparer());
Another way would be to override equality of your own class UserRoleListViewModel
but that affects everything and not just this Except
method. 另一种方法是覆盖你自己的类
UserRoleListViewModel
相等,但这会影响一切,而不仅仅是这个Except
方法。
Use StringComparison.OrdinalIgnoreCase
使用
StringComparison.OrdinalIgnoreCase
bool equals = string.Equals("Test", "test", StringComparison.OrdinalIgnoreCase);
If you need to use this in Except
method, you should create class which implements IEqualityComparer
(see this page ) and use string.Equals("Test", "test", StringComparison.OrdinalIgnoreCase)
in that class to compare two complex objects. 如果需要在
Except
方法中使用它,则应创建实现IEqualityComparer
类(请参阅本页 )并在该类中使用string.Equals("Test", "test", StringComparison.OrdinalIgnoreCase)
来比较两个复杂对象。 Becasue Linq does not know according to what you are comparing data. Becasue Linq不知道你根据什么比较数据。
By default it checks the results of GetHashCode
and Equals
(implemented for any object) and the default implementation for a reference type is on the reference itself. 默认情况下,它会检查
GetHashCode
和Equals
的结果(为任何对象实现),并且引用本身的引用类型的默认实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.