Mohan Raj
Mohan Raj

Reputation: 47

How to join two entities

How to join two entities

my entity name customer

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

I got result

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

how to join two entities in using linq
output exepected for single entity

Upvotes: 2

Views: 356

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176956

EDIT

As per the comment you want to join this in one entity called output , so you can do it with the help of anonymous type and join

 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
                  };

join will work like this

Check for more detail : SQL to LINQ ( Visual Representation )

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

image presetnation enter image description here

or

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;

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

For first

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

I thing GetAll() must return collection or enumerable of customers. Isn't it? Then, i thing you need union instead join!

Upvotes: 1

Related Questions