Linq中如何获得LEFT OUTER JOIN?如何获得、Linq、LEFT、JOIN

由网友(微笑吧趁现在还有牙齿)分享简介:我在我的数据库中的两个表看起来像:I have two tables in my database that look like that:客户:C_ID city--------------1 Dhaka2 New york3 London的personal_info:Per...

我在我的数据库中的两个表看起来像:

I have two tables in my database that look like that:

客户:

C_ID  city
--------------
1     Dhaka
2     New york
3     London

的personal_info:

Personal_Info:

P_ID  C_ID  Field       value
-------------------------------
1     1     First Name  Nasir
2     1     Last Name   Uddin
3     2     First Name  Jon
4     3     First Name  Lee

我需要一个选择的结果这样的:

I need a select result like that:

C_ID =1:

C_ID  Name (First Name + Last Name)  City
------------------------------------------
1     Nasir Uddin                    Dhaka

C_ID ='2':

C_ID  Name (First Name + Last Name)  City
---------------------------------------------
2     Jon                            New york

如何将相应的LINQ查询是什么样子?

How would the corresponding Linq query look like?

感谢 Nahid

Thanks Nahid

推荐答案

继previous答案如的 LINQ到SQL:多个左外连接可以看到结构解决这个例如是这样的:

Following a previous answer such as Linq to Sql: Multiple left outer joins you can see the structure for solving this eg something like:

var result = from customer in customers  
                   from personalFirst in personal  
                       .Where(pf => pf.Field == "First Name" && pf.C_ID == customer.C_ID)  
                       .DefaultIfEmpty() 
                   from personalLast in personal  
                       .Where(pl => pl.Field == "Last Name" && pl.C_ID == customer.C_ID)  
                       .DefaultIfEmpty()  
                    where customer.C_ID == 2  
                    select new { customer.C_ID, Name = (personalFirst != null ? personalFirst.Value : "") + " " + (personalLast != null ? personalLast.Value : "") };  

显然,如果你想要的所有记录,然后卸下C_ID = 2

Obviously if you want all records then remove the restriction on C_ID = 2

阅读全文

相关推荐

最新文章