views:

156

answers:

5

Hello all

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.

A: 

Does this meet your needs?

Master = new Master() { Field1 = v.Field1, Field2 = v.Field2, Field3 = v.Field3 }
John Fisher
Yes, exactly, but I want to avoid construct each field manually
igor
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?
John Fisher
+1  A: 

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

KristoferA - Huagati.com
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
igor
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).
KristoferA - Huagati.com
A: 

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.

xandy
A: 

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 )
Alex Black
it will cause problems when I would try to share dataContext or use external one. Exception will be thrown.
igor
A: 

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

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