在DBML中,如何获取具有特定字段而非所有字段的子表

时间:2019-01-21 13:50:42

标签: asp.net sql-server dbml

我们正在使用SQL Server,并且应用程序是使用 DBML 开发的。现在由于急切加载的功能,我遇到了一个问题。我有一个表A ,它与表B 有关系。 ( A-> B )。现在,当我尝试加载表A时,它将获得表B的所有字段。罪魁祸首是表B有2-3列,非常重,包含byte array数据,由于这些列,它也占用了同样,获取表A的数据也很麻烦。

问题
获得表A时,是否可以只加载表B的几列(不是所有列)?

我尝试过的事情
我收到此错误:

  

指定的表达式必须采用p.A的形式,其中p是   参数,A是属性或字段成员。

当我尝试以下代码时-

DataContext2.DeferredLoadingEnabled = false;
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<A>(x => x.Campaign);
options.LoadWith<A>(x => x.AnotherTable);
options.LoadWith<A>(x => x.B.Select(o => new B
{
    Id = o.Id,
    Name = o.Name,
    Person = o.Person,
}).ToList());
DataContext2.LoadOptions = options;

1 个答案:

答案 0 :(得分:1)

仅使用joinselect仅必要的列:

var yourQuery = (from t_A in dbContext.Table_A
             join t_B in dbContext.Table_B on t_A.ID equals t_B.ID                 
             //use where operator to filter rows
             select new {
                 Id = t_B.Id,
                 Name = t_B.Name,
                Person = t_B.Person
                  // any field you want from your query
             }).ToList();
相关问题