如何比较实体C#中的数据表? [英] How to compare datatable in entity C#?

查看:72
本文介绍了如何比较实体C#中的数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较不同数据库的同一列?



我尝试过:



How to compare different database same column?

What I have tried:

var t1= dbcontext.entities.Person(branchcode,countrycode).tolist();
//db change code
var t1= dbcontext.entities.Student(branchcode,countryclde).tolist();

varcommon=t1.Intersect(t2);

推荐答案





如果您的列表t1和t2是字符串列表,那么您可以通过这种方式轻松实现:

Hi,

If your lists t1, and t2 are a list of strings then it is so easy you can do it in this way:
var t1= dbcontext.entities.Student(branchcode,countrycode).tolist();
//db change code
var t1= dbcontext.entities.Student(branchcode,countryclde).tolist();

IEnumerable<string> differenceQuery =  
          t1.Except(t2);





如果不是,最好首先覆盖equals方法或更改你的linq查询以便更精确。



另一个例子可以在:

如何:查找设置差异两个列表(LINQ)(C#)



这里更复杂的例子。



如果这不能解决你的问题那么请发表评论我会帮助你改进我的解决方案,直到问题解决。



干杯,

AH



if not it is better to first override the equals method or change your linq query in order to be more precise.

Another example can be found in :
How to: Find the Set Difference Between Two Lists (LINQ) (C#)

Go here more complex example.

If this did not solve your problem then please leave a comment and I will assist you by improving my solution until your problem gets solved.

Cheers,
AH


这是我过去用来比较两个实体的东西,无论数据库是什么,只要它们具有相同的列名和表na mes。



返回值不同的列(即旧的与新的价值观)



注意:互联网上有很多这样的例子,有不同的变化等等。



This is something I have used in the past to compare two entities, regardless of database, as long as they have the same column names and table names.

It returns the columns with values that are different (i.e. old vs. new values)

Note: plenty of examples of this on the internet, with different variation, etc.

 public static List<PropertyInfo> CompareTo<T>(this T entityA, T entityB ) where T : class 
{
    var propertyInfoDifferences = new  List<PropertyInfo>();

    if(entityA == null || entityB == null) return propertyInfoDifferences;
    if (entityA.GetType().Name != entityB.GetType().Name) return propertyInfoDifferences;

    var properties = entityA.GetType().GetProperties();

    foreach (var propertyInfo in properties)
    {
        if (propertyInfo == null) continue;

        var entityAProperty = entityA.GetType().GetProperty(propertyInfo.Name);
        var entityBProperty = entityB.GetType().GetProperty(propertyInfo.Name);

        if(entityAProperty == null || entityBProperty == null) continue;

        var entityAPropertyValue = entityAProperty.GetValue(entityA);
        var entityBPropertyValue = entityBProperty.GetValue(entityB);

        if (entityAPropertyValue == null && entityBPropertyValue == null) continue;

        string entityAStringValue = null;
        string entityBStringValue = null;

        if (entityAPropertyValue != null)
        {
            entityAStringValue = entityAPropertyValue.ToString();
        }

        if (entityBPropertyValue != null)
        {
            entityBStringValue = entityBPropertyValue.ToString();
        }

        if (entityAStringValue != entityBStringValue)
        {
            propertyInfoDifferences.Add(propertyInfo);
        }
    }

    return propertyInfoDifferences;
}





实施可能是这样的:





Implementation could be something like this:

List<PropertyInfo> propertyDiffs = entityA.CompareTo(entityB);


这篇关于如何比较实体C#中的数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆