使用LINQ在1个类中组合2个表

时间:2011-07-13 12:51:00

标签: c# linq linq-to-sql associations linq-to-objects

我有2个班级:

[Table(Name = "_Reference2")]
public class Customer
{
    [Column(Name = "_IDRRef")]
    public Binary CustomerRef { get; set; }
    [Column(Name = "_Description")]
    public string Name { get; set; }
}

[Table(Name = "_AccumReg2210")]
public class Payment
{
    [Column(Name="_Period")]
    public DateTime Period { get; set; }
    [Column(Name = "_Fld2211RRef")]
    public Binary Customer { get; set; }
    [Column(Name = "_Fld5051RRef")]
    public decimal Sum { get; set; }
}

有没有机会在1节课中合并这些课程?通常,我需要将 JOIN (ON Customer.CustomerRef = Payment.Customer)的查询写入此类。最好是实现连接到两个表的复杂类。

3 个答案:

答案 0 :(得分:2)

var results = from c in Customers
              from p in Payments
              where p.Customer == c.CustomerRef
              select new ComplexObject() {
                 Name = c.Name,
                 ...
              };

答案 1 :(得分:0)

如果您不想使用Linq显式连接两个表,您可以在数据库中创建一个View并使用它生成一个模型(使用LinqToSQL或实体框架)?

答案 2 :(得分:0)

我不确定这是否适用于您所需的解决方案,但您是否考虑过一个简单的CustomerPaymentViewModel类?

public class CustomerPaymentViewModel
{
    public Customer { get; set; }
    public Payment { get; set; }
}