0

你好,

我举个经典的例子,我有一张Customer桌子和Purchase一张桌子,一个客户可以有几个订单。

我创建了一个DBML文件,然后从两个表的“服务器资源管理器窗口”拖放到 DBML 编辑器,生成了两个实体:CustomersPurchases

我有一些对象:

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

任何想法 ?

谢谢,

4

2 回答 2

0

It doesn't appear that you have any translation logic between Purchase and MyPurchase.

Also the the Cast<>() method will try to use implicit casting, but unless your Linq-to-SQL entities implement that interface, it cannot be cast in that way.

My suggestion would be to use something like AutoMapper or add translation logic in your linq statement (similar to what you're doing with MyCustomer). Something like:

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
            .Where(x => x.IdCustomer == customer.Id)
            .Select(x => new MyPurchase {
                Id = x.Id,
                Code = x.Code,
                CreationDate = x.CreationDate
            }).ToList();
    };
于 2012-07-04T06:32:15.920 回答
0

Here is a sample, and where is your customer Id in the Purchase

    var items = (from p in _dataContext.Purchases
                 join c in _dataContext.Customers
                 on p.CustomerID equals c.ID
                 where (c.ID == customerId)
                 orderby p.CreationDate descending
                 select new { p, c });

foreach (var data in items)
{
    // do stuff
    int customerId = data.c.ID;
    int purchaseId = data.p.ID;
    // you can acces all properties of both objects
}
于 2012-07-04T06:33:31.960 回答