0

That's example

SQL

create view v_join 
as select m.* , d.oneDetail from master m, detail d 
where m.key = d.key

LINQ

var view = from v in dc.v_join select new
{
   Master = ???? /// that is an issue, how can I construct the objects using same fields but from another query,
   Detail = v.oneDetail

};

foreach (var entry in view)
{
  Master mEntry =  entry.Master; // then we can use it, there are no problems
}

This situation meets rather often and is there some tricky way to construct the object using same fields but from another view, and uses.

When I make such join at C#-side, I get low performance, issues with lazy loads etc. And anyway LINQ issues many queries to fetch each detail record, is is inacceptible at the performance point of view.

There must be obvious solution, I cannot believe nobody faced same problem. All obvious solutions like dc.Translate do almost same but not exactly what I need.

Help appreciated.

0

5 Answers 5

1

from m in dc.Master join d in dc.Detail on m.Key = d.Key select new { m, d };

Sign up to request clarification or add additional context in comments.

2 Comments

I tried such approach, judging on that further processing of result is extremely slow, I suppose LINQ issues separate query to fetch each detail record
It doesn't. The query above will result in a single hit. If you encounter performance problems, please provide more details (e.g. db schema, table details etc).
0

Does this meet your needs?

Master = new Master() { Field1 = v.Field1, Field2 = v.Field2, Field3 = v.Field3 }

2 Comments

Yes, exactly, but I want to avoid construct each field manually
I'm not sure I understand. You want a high-performance solution, but you don't want to use this approach. Have you tested it to see if it is fast enough? If so, then why not use this the constructor?
0

To me this sounds strange. The reason why you use view is to have different "view" on the model, rather than accessing the raw model as well.

I suggest you stay joining the master-detail table in LINQ if you really need so, you can get rid of the lazy loading by using the loadWith Options. While correctly tweaked, LINQ can do the eager loading which solve the performance issue.

Comments

0

Try using DataLoadOptions and LoadWith, see: http://dotnet.org.za/hiltong/archive/2008/02/12/lazy-loading-in-linq-loadwith-and-associatewith-dataloadoptions-part-1-loadwith.aspx

E.g.

System.Data.Linq.DataLoadOptions dl = new System.Data.Linq.DataLoadOptions();
dl.LoadWith<Detail>( detail => detail.Master )

1 Comment

it will cause problems when I would try to share dataContext or use external one. Exception will be thrown.
0

Can't you do from v in dc.v_join select new { Master = new { Column1Name = v.masterColumn1, Column2Name = v.masterColumn2, ... }, Detail = v.oneDetail }; ?

1 Comment

Obvious approach, but I'd like to avoid explicit construction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.