0

I want to find the common ones from the values in different database. And include different database values insert other database.

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

But not take common.How to solve except common value problem.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You want to remove both common entries or want to take one? – Just code Nov 14 '18 at 09:27
  • I would like to query whether these fields exists in common.ı want to add these fields later other table. –  Nov 14 '18 at 09:30
  • ı use except because ı know two tables take except. –  Nov 14 '18 at 09:33
  • 1
    I have the feeling you don't understand what [`Except()`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.except) does. – user247702 Nov 14 '18 at 09:35
  • I use the except method to find out if it is in a table and not in the other tables.Is wrong? I dont know union.how can ı use union.I want to find differences between two tables. –  Nov 14 '18 at 09:40
  • Here is a good sumary of Set operation: [docs.microsoft](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/set-operations). – Drag and Drop Nov 14 '18 at 10:12
  • To compare Object on specif field you can use the `IEqualityComparer` implementation you have in the Expect (documentation)[https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.7.2]. (The second code block) – Drag and Drop Nov 14 '18 at 10:15
  • @DragandDrop the compiler provides `Equals` and `GetHashCode` implementations for anonymous types, see [this related question](https://stackoverflow.com/questions/543482/linq-select-distinct-with-anonymous-types). – user247702 Nov 14 '18 at 11:18
  • @Stijn, Good to know. At least I've provided clear doc on set operation and draw so circle with color to make it clear. But the fact that it works because of anon type make me wonder what is op real question... – Drag and Drop Nov 14 '18 at 12:35
  • @S.C, Perhaps you are trying to merge two table from different database. I will recommend doing it in SQL directly. As linq is for querry and have poor performance on update/insert. A simple merge sattement could be what you are looking for. https://stackoverflow.com/questions/40385106/merge-2-tables-from-different-databases – Drag and Drop Nov 14 '18 at 12:37
  • Ho revelation, Op talked about "common element" meaning Intersection `(A ∩ B)` so when doing the expect it doesn't has the irrevelant elments.. `InsertInB = A - (A ∩ B)` and `InsertInA = B - (A ∩ B)`. `a.Except(a.Intersect(b));` – Drag and Drop Nov 14 '18 at 12:50

1 Answers1

1

Choosing from set operation, you want:

Except:

Produces the set difference of two sequences, the elements of one collection that do not appear in a second collection.

a.Except(b); // a U b 

enter image description here

To compare set of object of some custom field, you have to implement 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());
Community
  • 1
  • 1
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37