2013-09-24 60 views
-5

如何低於內轉換加入SQL語句轉換成LINQ語句在LINQ中進行內部連接?

SELECT ca.[CUS_ADDRESS_ID] 
    ,ca.MASTER_CUSTOMER_ID  
    ,ca.[ADDRESS_1]  
    ,[CITY] 
    ,[STATE] 
    ,COUNTRY_CODE 
    ,cad.ADDRESS_TYPE_CODE 
    ,cad.ADDRESS_STATUS_CODE 
    inner join [CUS_ADDRESS_DETAIL] cad on ca.CUS_ADDRESS_ID = cad.CUS_ADDRESS_ID and cad.PRIORITY_SEQ = 0 
    where ca.CUSTOMER_ID = '0000026' 

,並分配給

public class Location 
{  
    public string CustomerNumber { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Country { get; set; } 
    public string AddressLocCode { get; set; } 
    public string AddressStatus { get; set; } 
} 
+1

http://stackoverflow.com/questions/37324/what-is-the-syntax-for-an-inner-join- in-linq-to-sql?rq = 1 –

回答

0

查詢也將結束看起來像這樣(記住,雖然,你會必須改變你的字段名相匹配,因爲它們看起來有點錯位):

var locations = from c in Customers 
       join ca in CustomerAddresses 
       on new { c.CustomerAddressId, 0 } 
        equals new { ca.CustomerAddressId, ca.PrioritySeq } 
       select new Location 
       { 
        CustomerNumber = c.MasterCustomerId, 
        City = ca.City, 
        State = ca.State, 
        Country = ca.Country, 
        AddressLocCode = ca.AddressLocCode, 
        AddressStatus = ca.AddressStatus 
       }; 
+0

如果我想在ca.CUS_ADDRESS_ID = cad.CUS_ADDRESS_ID和cad.PRIORITY_SEQ = 0上添加'inner join [CUS_ADDRESS_DETAIL] cad。這個怎麼做? – James123

+0

@ James123 - 更新。 –

+0

它說:「無法從查詢中推斷出類型參數」cadidates is system.collections.generic.IEnumerable join James123