LINQ:从第二个表中预取数据[英] LINQ: Prefetching data from a second table

本文是小编为大家收集整理的关于LINQ:从第二个表中预取数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用LINQ查询来预测一些外键数据.一个快速解释我的问题的例子如下:

var results = (from c in _customers
               from ct in _customerTypes 
               where c.TypeId == ct.TypeId 
               select new Customer
                          {
                             CustomerId = c.CustomerId,
                             Name = c.Name,
                             TypeId = c.TypeId,
                             TypeName = ct.TypeName,  <-- Trying to Prefetch this
                          }).ToList();

客户类看起来像:

[Table(Name = "Customers")]
public class Customer
{
   [Column(Name = "CustomerId", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
   public int CustomerId { get; set; }

   [Column(Name = "Name")]
   public string Name { get; set; }

   [Column(Name = "TypeId")]
   public int TypeId { get; set;}

   public string TypeName { get; set; }

   public Confession (){}
}

但是,linq不允许您在不允许的" Quarly type'客户'的明确构造''中,不允许您这样做.

我显然正在错误地接近这一问题.任何朝正确方向的指针都将是最有帮助的.

推荐答案

正如它所说的,您无法在那里构建客户.

(可以说)最简单的事情是创建一个封装您需要的属性的新类.您可以通过这样的方式从客户类中获得所有内容:

var results = (from c in _customers
               from ct in _customerTypes 
               where c.TypeId == ct.TypeId 
               select new
                      {
                         Customer = c,
                         TypeName = ct.TypeName
                      }).ToList();

其他推荐答案

如果要进行真正的预加载,则可以执行以下操作:

(这假设您的数据库中的Customer和CustomerType表之间有一个联接,而linq到SQL知道了.)

MyDataContext dc = new MyDataContext(); // Use your application-specific DataContext class
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Customer>(c => c.CustomerType);
dc.LoadOptions = loadOptions;
var results = from c in dc.GetTable<Customer>() select c;

然后您可以访问TypeName因此(其中c是c in results in results):

c.CustomerType.TypeName;

本文地址:https://www.itbaoku.cn/post/1557075.html

问题描述

I'm trying to pre-fetch some foreign key data using a linq query. A quick example to explain my problem follows:

var results = (from c in _customers
               from ct in _customerTypes 
               where c.TypeId == ct.TypeId 
               select new Customer
                          {
                             CustomerId = c.CustomerId,
                             Name = c.Name,
                             TypeId = c.TypeId,
                             TypeName = ct.TypeName,  <-- Trying to Prefetch this
                          }).ToList();

The Customer class looks like:

[Table(Name = "Customers")]
public class Customer
{
   [Column(Name = "CustomerId", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
   public int CustomerId { get; set; }

   [Column(Name = "Name")]
   public string Name { get; set; }

   [Column(Name = "TypeId")]
   public int TypeId { get; set;}

   public string TypeName { get; set; }

   public Confession (){}
}

However LINQ will not let you do this throwing a NotSupportedException with "Explicit construction of entity type 'Customer' in query is not allowed."

I'm clearly approaching this incorrectly. Any pointers in the right direction would be most helpfull.

推荐答案

As it says, you can't construct a Customer there.

The (arguably) easiest thing to do would be to create a new class that encapsulates the properties you need. You can get everything from the Customer class by doing it like this:

var results = (from c in _customers
               from ct in _customerTypes 
               where c.TypeId == ct.TypeId 
               select new
                      {
                         Customer = c,
                         TypeName = ct.TypeName
                      }).ToList();

其他推荐答案

If you want to do genuine pre-loading, you can do the following:

(This assumes there's a join between the Customer and CustomerType tables in your database, and that LINQ to SQL knows about it.)

MyDataContext dc = new MyDataContext(); // Use your application-specific DataContext class
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Customer>(c => c.CustomerType);
dc.LoadOptions = loadOptions;
var results = from c in dc.GetTable<Customer>() select c;

You could then access the TypeName thus (where c is a Customer in results):

c.CustomerType.TypeName;