c# - 如何解决c#中的实体问题?
问题描述
我想从不同数据库中的值中找到常见的值。并包含不同的数据库值插入其他数据库。
var t1 = dbcontext.Entities
.Student
.Select(c => new { countrycode = c.countrycode,
branchcode = c.branchcode });
// db changes code
var t2 = dbcontext.Entities
.Student
.Select(c => new { countrycode = c.countrycode,
branchcode = c.branchcode });
var common = t2.Except(t1);
List<newtable> mn = new List<newtable>();
但不取共性。除了共性值问题如何解决。
解决方案
从set operation中选择,您想要:
除了:
产生两个序列的集合差,一个集合的元素没有出现在第二个集合中。
a.Except(b); // a U b
要比较某些自定义字段的对象集,您必须实现IEqualityComparer<T>
.
public class Student
{
public string Name { get; set; }
public int CountryCode { get; set; }
public int BranchCode { get; set; }
public int Code { get; set; }
}
public class StudentComparer : IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
{
if (Object.ReferenceEquals(x, y)) return true;
return x != null
&& y != null
&& x.CountryCode.Equals(y.CountryCode)
&& x.BranchCode.Equals(y.BranchCode);
}
public int GetHashCode(Student obj)
{
int hashCountryCode = obj.CountryCode.GetHashCode();
int hashBranchCode = obj.BranchCode.GetHashCode();
return hashCountryCode ^ hashBranchCode;
}
}
var toInstertInB = a.Except(b, new StudentComparer());
推荐阅读
- azure - 如何将数据磁盘附加到 Windows Server Azure VM 并直接在模板中对其进行格式化?
- php - 为什么这个查询总是返回 HY093 错误
- php - 如何在codeigniter中发送多个具有相同名称属性的选择框值
- typescript - 在 Angular 5 中使用 RxJS 轮询多个资源
- python - 标签存在于 HTML 代码中,但无法通过 xpath 访问
- exiftool - 使用 exiftool 将地理标记的图像保存到新目录
- javascript - 使用过滤器过滤对象
- datepicker - Datepicker Materializecss 禁用天数功能
- javascript - ASP.NET JQuery - jQuery.Deferred 异常:$ 不是函数
- javascript - Javascript 打印 node.js 版本和模块版本