简体   繁体   中英

LINQ to SQL in c# and database access

I do not know how to ask this question. So I will just give an example.

Code:

var db = new dbContext();
var dlo = new DataLoadOptions()

dlo.LoadWith<Order>(x => x.Company);
db.LoadOptions = dlo;
var compIds = prms.companies.Select(x => x.Id).ToArray();

as I understand with above code I load Company from Order table and then getting companies by their ID's. Is it same as

var compIds = (from it in context.GetTable<Order>()
                       select it.Company.Id).ToArray();

? Or am I totally confusing two different concepts?

I think you are confused about three things:

  1. context.GetTable<Order>() is the same as context.Orders (assuming everything is configured correctly.

  2. The queries you've provided as examples are not equivalent; they ask two different questions. The first is asking "Give me all the Company IDs". The DataLoadOptions won't even be used. The second is asking Give me every Order's Company ID. You will get duplicates.

  3. DataLoadOptions is generally used for eager loading of related object instances, not joining.

I think what you are really looking for is:

var uniqueCompanyIDsThatHaveAtLeastOneOrder = db.Orders.Select(o => o.Company.Id).Distinct();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM