LINQ to SQL的LoadWith限制领域返回 [英] linq to sql LoadWith limiting fields returned
问题描述
有没有办法使用LoadWith,但指定要回来了?
例如,如果我有两个表1)产品2)分类
和做一些像
DataLoadOptions DLO =新DataLoadOptions();
dlo.LoadWith<产品>(D = GT; d.Categories);
db.LoadOptions = DLO;
MyDataContext DB =新MyDataContext();
VAR的结果=从db.Products
D选择D组;
当我检查查询在探查我看到正在返回的所有从分类表中的行。我真正需要的是名称字段。
我知道我可以使用联接重写查询,但我需要返回的结果集作为产品的数据类型这就是为什么我使用LoadWith。
没有那是不可能的LoadWith。
您可以尝试用在投影嵌套查询,尽管这将是缓慢的(所以装1查询每个产品相关类别)每个父1查询。
Is there a way to use LoadWith but specify the fields that are returned?
For example, if I have two tables 1) Products 2) Categories
and do something like
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Products>(d => d.Categories);
db.LoadOptions = dlo;
MyDataContext db = new MyDataContext();
var result = from d in db.Products
select d;
when i check the query in profiler i see that ALL the rows from the Categories table are being returned. All I really need is the "Name" field.
I know I can rewrite the query using joins but I need to return the result set as a "Product" data type which is why I am using LoadWith.
No that's not possible with LoadWith.
You could try with a nested query in the projection, though that will be slow: 1 query per parent (so 1 query for the related category per product loaded).
这篇关于LINQ to SQL的LoadWith限制领域返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!