你好,
我举个经典的例子,我有一张Customer
桌子和Purchase
一张桌子,一个客户可以有几个订单。
我创建了一个DBML
文件,然后从两个表的“服务器资源管理器窗口”拖放到 DBML 编辑器,生成了两个实体:Customers
和Purchases
我有一些对象:
public interface IMyCustomer
{
int Id { get; set; }
string Code { get; set; }
string Firstname { get; set; }
string Lastname { get; set; }
List<IMyPurchase> Purchases { get; set; }
}
public class MyCustomer : IMyCustomer
{
public int Id { get; set; }
public string Code { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public List<IMyPurchase> Orders { get; set; }
}
public interface IMyPurchase
{
int Id { get; set; }
string Code { get; set; }
DateTime CreationDate { get; set; }
}
public class MyPurchase : IMyPurchase
{
public int Id { get; set; }
public string Code { get; set; }
public DateTime CreationDate { get; set; }
}
var result =
from customer in _dataContext.Customers
join order in _dataContext.Purchases on customer.Id equals order.IdCustomer
select new MyCustomer
{
Code = customer.Code,
Firstname = customer.Firstname,
Lastname = customer.Lastname,
Id = customer.Id,
Purchases = _dataContext.Purchases
.Select(x => x.IdCustomer == customer.Id)
.Cast<IMyPurchase>()
.ToList<IMyPurchase>()
};
linq 查询工作。对于结果条目,我有关于的所有信息customer
,我有正确的行数,Purchases
但每个条目的每个属性Purchases
都是空的。我想加载Customer
信息和所有purchase
任何想法 ?
谢谢,