简体   繁体   English

LINQ to SQL in c#和数据库访问

[英]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. 正如我对上面的代码所理解的,我从Order表中加载Company,然后通过他们的ID获取公司。 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. context.GetTable<Order>()context.Orders相同(假设一切都配置正确。

  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". 首先是询问“给我所有的公司ID”。 The DataLoadOptions won't even be used. 甚至不会使用DataLoadOptions The second is asking Give me every Order's Company ID. 第二个是要求给我每个订单的公司ID。 You will get duplicates. 你会得到重复的。

  3. DataLoadOptions is generally used for eager loading of related object instances, not joining. DataLoadOptions通常用于热切加载相关对象实例,而不是加入。

I think what you are really looking for is: 我认为你真正想要的是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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