如何加入两个实体

时间:2012-11-21 11:22:16

标签: linq linq-to-entities entity-framework-4.1 linq-to-objects

如何加入两个实体

我的实体名称客户

Public Class Customer
{
   Name string {get;set;}
   Address string {get;set;}
}

我得到了结果

Customer customer = this.customerService.GetAll();
Customer person = this.supplierService.GetAll();

如何使用linq加入两个实体
    单个实体的输出

2 个答案:

答案 0 :(得分:1)

修改

根据您要在一个名为output的实体中加入此注释,因此您可以在匿名类型的帮助下加入

 var data = from c in customer 
               join p in person 
               on p.ID equals c.ID
               select new 
                {
                       PersonName = p.Name,
                       CustomerName - c.Name
                       PersonAdd = p.Add
                       CustomerAdd = c.Add
                  };

加入将像这样工作

查看更多详情:SQL to LINQ ( Visual Representation )

var data = from c in customer 
           join p in person 
           on p.ID equals c.ID
           select c;

图像预设 enter image description here

Linq Join on Mutiple columns

var cust = from c in Customers
           join p in persons on
             new { Name= c.Name, Address= c.Address }    
           equals
             new { Name= p.Name, Address= p.Address }    
           select c;

答案 1 :(得分:1)

首先

Customer customer = this.customerService.GetAll();
Customer person = this.supplierService.GetAll();

我的事物GetAll()必须返回收集或可枚举的客户。不是吗? 然后,我需要工会而不是加入!