How to match two Lists with only items that are different in Linq -
i have studentdata class
public class studentdata { public int id { get; set; } public string firstname { get; set; } public string surname { get; set; } public int? bonus { get; set; } public int? subject1mark { get; set; } public int? subject2mark { get; set; } public int? subject3mark{ get; set; } } each student has unique id identifies him
i have list<studentdata> currentdata has data of
1, john, smith, 10, 50 ,50 ,50
2, peter, parker, 10, 60 ,60 ,60
3, sally, smart, 10, 70 ,70 ,70
4, danny, darko, 20, 80, 80, 80
i have list<studentdata> datatoupdate contains id , marks fields. not other fields.
1, null, null, null, 50 ,50 ,50
2, null, null, null, 65, 60 ,60
3, null, null, null, 70 ,70 ,70
the ids of list not necessary in same order
if compare 2 lists peter parker's marks have changed in 1 subject.
i want output return
2, peter, parker, 10, 65 ,60 ,60
i want takelist<studentdata> currentdata inner join list<studentdata> datatoupdate marks different
so in sql want following
select currentdata.id, currentdata.firstname , currentdata.surname, currentdata.bonus, datatoupdate.subject1mark, datatoupdate.subject2mark, datatoupdate.subject3mark currentdata inner join datatoupdate on currentdata.id= datatoupdate.id , ( currentdata.subject1mark<> datatoupdate.subject1mark or currentdata.subject2mark<> datatoupdate.subject2mark or currentdata.subject3mark<> datatoupdate.subject3mark ) how do above in linq?
in linq select how take properties currentdata include 3 subject properties datatoupdate in give me list<changeddata>?
i map each , every property studentdata has 100 fields , prefer have
select new studentdata { this=currentdata, this.subject1mark=datatoupdate.subject1mark, this.subject2mark=datatoupdate.subject2mark, this.subject3mark=datatoupdate.subject3mark, } but i'm not sure how write this
there answer in stackoverflow question should work doesn't. if implement solution (i simplify example simplicity)
var changeddata = currentdata .join(datatoupdate, cd => cd.id, ld => ld.id, (cd, ld) => new { cd, ld }) .select(x => { x.cd.subject1mark= x.ld.subject1mark; return x.cd; }) ; but above x.cd.subject1mark isn't updated x.ld.subject1mark although use answer in linked stackoverflow question
the structure of linq query looks similar sql:
var res = cur in currentdata join upd in datatoupdate on upd.id equals cur.id (cur.subject1mark != upd.subject1mark || cur.subject2mark != upd.subject2mark || cur.subject3mark != upd.subject3mark) select new { current = cur , updatedsubject1mark = upd.subject1mark , updatedsubject2mark = upd.subject2mark , updatedsubject3mark = upd.subject3mark }; the main difference filtering out inequality has moved on clause in sql where clause of linq.
Comments
Post a Comment