Thursday, 15 January 2015

sql - Left Join using Linq with Lambda Expressions returning the Left table -



sql - Left Join using Linq with Lambda Expressions returning the Left table -

i seek create lambda look every case in folder specific application. have 2 tables:

table 1: cases

caseid | folderid | casecontent 0 1 "blap" 1 2 "blop" 2 1 "blup" 3 3 "blip"

table2: folders

folderid | applicationid 0 1 1 1 2 1 3 2

i want homecoming every case thats in folder applicationid 1.

result:

caseid | folderid | casecontent 0 1 "blap" 1 2 "blop" 2 1 "blup"

i found nice illustration of bring together look here: http://www.c-sharpcorner.com/uploadfile/54db21/inner-join-using-linq-with-lambda/

the reason why can't utilize because returns content both tables

var query = objentities.employee.join(objentities.department, r => r.empid, p => p.empid, (r,p) => new{r.firstname, r.lastname, p.departmentname});

so instead of

(r,p) => new{r.firstname, r.lastname, p.departmentname}

i want homecoming entire 'r' , nil of 'p'.

i tryed in lines of

iqueryable<case> cases = cases.join(folders, f => f.folder.folderid, c => c.folderid, (c) => c);

needles doesn't work. :(

thanks in advance!!

well theres several ways this.

but bring together look right you'd want

iqueryable<case> cases //line:#1 = cases.join(folders.where(f => f.applicationid == 1), //line:#2 c => c.folderid, //line:#3 f => f.folderid, //line:#4 (f,c) => c); //line:#5

explanation:

.join takes 4 parameters. (see msdn total description)

1st collection joining 2 (as on line 2 above). want subset of folders has applicationid equal 1, hence .where(f => f.applicationid == 1) 2nd look identifies key joining on in outer collection (cases) (as on line 3 above) 3rd look identifies key joining on in inner collection (folders) (as on line 4 above) 4th look selects results 2 joined collections. left hand side of look needs take 2 parameters represent 2 sides of join, , right hand side can combination of these. (as on line 5 above)

i prefer using linq look though like:

var query = c in cases bring together f in folders on c.folderid equals f.folderid f.applicationid == 1 select c;

think that's bit more readable!

sql linq lambda left-join

No comments:

Post a Comment