c# – 如何在linq中比较两个不区分大小写的复杂对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何在linq中比较两个不区分大小写的复杂对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1676字,纯文字阅读大概需要3分钟。
内容图文
![c# – 如何在linq中比较两个不区分大小写的复杂对象](/upload/InfoBanner/zyjiaocheng/806/04edc8de40c74584b03e0558ce8e6402.jpg)
我必须列出对象.所以我需要比较这些对象,并从“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”
解决方法:
要将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());
另一种方法是覆盖你自己的类UserRoleListViewModel的相等,但这会影响一切,而不仅仅是这个Except方法.
内容总结
以上是互联网集市为您收集整理的c# – 如何在linq中比较两个不区分大小写的复杂对象全部内容,希望文章能够帮你解决c# – 如何在linq中比较两个不区分大小写的复杂对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。