【发布时间】:2010-11-30 03:05:41
【问题描述】:
我正在尝试使用 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();
Customer 类如下所示:
[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 不允许您执行此操作并引发 NotSupportedException,并显示“不允许在查询中显式构造实体类型 'Customer'。”
我显然是在错误地处理这个问题。任何指向正确方向的指针都会很有帮助。
【问题讨论】:
标签: c# linq linq-to-sql