.net - sql Left join with linq lampda expression -



.net - sql Left join with linq lampda expression -

i have problem in making left/right bring together linq.

i have lets say

public class client { prop string customerid { get; set; } prop string languageguid { get; set; } } public class readonlycustomer { prop string customerid { get; set; } prop string languageguid { get; set; } }

i have lot of customers in readonlycustomer table. in case dont have customers in client table. cant utilize join, dont inner join. need left or right join.

var test = db.customer.join(db.readonlycustomer, p => p.customerid, o => o.customerid, (c, o) => new readonlycustomer() { customerid = c.customerid, languageguid = o.languageguid ?? c.languageguid });

at point, null pointer, because query cant bring together on null ref.

how can left bring together equal sql left join, null value not exist in datasource.

this needs in lampda not comprehensing syntax (from o in ....)

// dennis

you need utilize groupjoin, in conjunction phone call defaultifempty() afterwards give single null value group. can utilize selectmany() end 1 result per pair, noting 1 of values in result may null.

for example:

// note: can *left* join, "bigger" table comes first var test = db.readonlycustomer .groupjoin(db.customer, p => p.customerid, o => o.customerid, (p, os) => new { p, os = os.defaultifempty() }) // each pair may have multiple os here .selectmany(pair => pair.os.select(o => new { pair.p, o })) // flattened, o may null .select(pair => new readonlycustomer { customerid = pair.p.customerid, languageguid = o != null ? o.languageguid ?? p.languageguid : p.languageguid });

(out of interest, why need in lambda syntax rather query expression? typically joins of kinds simpler express in query expressions.)

.net linq

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -