c# - DefaultIfEmpty in LINQ-to-SQL join causing duplicates -



c# - DefaultIfEmpty in LINQ-to-SQL join causing duplicates -

here extract of query:

... bring together p in dc.pods on c.id equals p.consignment pg pgg in pg.defaultifempty() ...

what query should 'pods' associated consignment, store ienumerable object (which seems work) can run through when querying main ienumerable generated query.

the problem is, getting duplicate main rows defaultifempty line, happens when row has multiple pods - it's returning row each pod, incorrect. if take out pg.defaultifempty() line, seems work bit better, still want rows without pods.

any ideas guys?

forgive me if i'm off on intention because can't see finish construction of info or initial from or final select clause in query excerpt. i'm posting think solution based on snippet , sample info constructed. allow me know if i'm off , i'll right it.

if want list of rows of consignments pods, each consignment pod on own line, (keep in mind from , select clause based on sample data):

// select consignment id & name (i made up) , each matching pod var results = c in consignments bring together p in dc.pods on c.id equals p.consignment pg pgg in pg.defaultifempty() select new { id = c.id, name = c.name, pod = pgg }; // sample display kicks , grins foreach (var r in results) { console.writeline(r.name + " " + ((r.pod != null) ? (r.pod.consignment + " " + r.pod.description) : "none")); }

this query outputs like:

one 1 textbox 2 none 3 3 refridgerator 3 3 bucket 4 none 5 none

however i'm not quite sure understand remark:

"the problem is, getting duplicate main rows"

i'm not sure if you're saying don't want see 1 consignment per purchase per row each result in ienumerable item consignment , sequence of pods, you'd want query like:

// select consignment id , name (i made up), , list of pods // instead of individual pod var results = c in consignments bring together p in dc.pods on c.id equals p.consignment pg select new { id = c.id, name = c.name, pods = pg }; // sample display kicks , grins foreach (var r in results) { console.writeline(r.name + " "); if (r.pods.count() > 0) { foreach (var pod in r.pods) { console.writeline("\t" + pod.consignment + " " + pod.description); } } else { console.writeline("\tnone"); } }

where select selecting pod list instead of individual match, outputs this:

1 1 textbox 2 none 3 3 refridgerator 3 bucket 4 none 5 none

c# sql linq linq-to-sql

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -