【问题标题】:Inner join in LINQ? [duplicate]LINQ的内部连接? [复制]
【发布时间】:2013-09-24 15:03:44
【问题描述】:

如何将下面的内连接 SQL 语句转换为 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; }
}

【问题讨论】:

标签: c# linq linq-to-sql


【解决方案1】:

查询最终会看起来像这样(但请记住,您必须更改字段名称以匹配,因为它们看起来有点混乱):

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

【讨论】:

  • 如果我想在 ca.CUS_ADDRESS_ID = cad.CUS_ADDRESS_ID 和 cad.PRIORITY_SEQ = 0 上添加`内部连接 ​​[CUS_ADDRESS_DETAIL] cad。如何做到这一点?
  • 它说“无法从查询中推断类型参数”候选人是 system.collections.generic.IEnumerable join
猜你喜欢
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 2020-03-06
  • 2017-06-23
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多