Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
Posted
Updated 14-Nov-18 1:32am
v2
Comments
F-ES Sitecore 14-Nov-18 7:51am    
If your question is how to connect "dbcontext" to a different database then you can pass the connection string to the target database when you create the class

dbcontext = new DbContext("connection string here")

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);


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
 
Share this answer
 
v3
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.

C#
 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:

C#
List<PropertyInfo> propertyDiffs = entityA.CompareTo(entityB);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900