我很努力地將sql語句轉換成Linq。Linq多重連接 - 結果太多
我得到的結果太多,無法計算出正確的語法應該是什麼。
這是SQL:
SELECT
Client_Details_Legacy.FullName,
Client_Details_Legacy.AddressLine1,
Client_Details_Legacy.AddressLine2,
Client_Details_Legacy.AddressLine3,
Policy_Property.PolicyNumber,
Policy_Property.CoverTo,
Clients.Id
FROM
Clients
INNER JOIN Policy_Property
ON Clients.Id = Policy_Property.Client_Id
INNER JOIN Client_Details_Legacy
ON Clients.Client_Details_Legacy_Id = Client_Details_Legacy.Id
WHERE
(Policy_Property.CoverTo >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102))
AND (Policy_Property.CoverTo <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))
ORDER BY
Clients.Id
這是接近我設法讓我的LINQ的聲明,但它返回更多的行比它應該是。
我懷疑它的東西做加盟,但我似乎無法得到正確的語法:
var query = from clients in db.Clients
.Include("Client_Details_Legacy")
.Include("Policies_Property")
where clients.Client_Details_Legacy.Country.Code == countryCode
&& clients.Policies_Property.Any(x => x.CoverTo >= CoverToStart
&& x.CoverTo <= CoverToEnd)
select clients;
return query.ToList();
這是我的模型......
public class Client
{
public int Id { get; set; }
[Index]
[MaxLength(50)]
public string ClientNumber_Legacy { get; set; }
[Index]
[MaxLength(50)]
public string ClientNuber_Websure { get; set; }
public Client_Details_Enhanced Client_Details_Enhanced { get; set; }
public Client_Details_Legacy Client_Details_Legacy { get; set; }
public Client_Details_Websure Client_Details_Websure { get; set; }
public List<Policy_Motor> Policies_Motors { get; set; }
public List<Policy_Property> Policies_Property { get; set; }
}
public class Policy_Property
{
[Key]
[MaxLength(50)]
public string PolicyNumber { get; set; }
public Policy_Property_Details_Legacy Policy_Property_Details_Legacy { get; set; }
public Policy_Property_Details_Enhanced Policy_Property_Details_Enhanced { get; set; }
public DateTime CoverFrom { get; set; }
public DateTime CoverTo { get; set; }
public List<Insured_Property> Insured_Properties { get; set; }
}
public class Client_Details_Legacy
{
public int Id { get; set; }
public ws_lookup_ClientType ClientType { get; set; }
[DisplayName("Title")]
public ws_lookup_Title Title { get; set; }
[Index]
[DisplayName("Full Name")]
[MaxLength(250)]
public string FullName { get; set; }
public string NationalIdNumber { get; set; }
public string EmailAddress { get; set; }
[DisplayName("Address Line 1")]
[MaxLength(250)]
public string AddressLine1 { get; set; }
[DisplayName("Address Line 2")]
[MaxLength(250)]
public string AddressLine2 { get; set; }
[DisplayName("Address Line 3")]
[MaxLength(250)]
public string AddressLine3 { get; set; }
[DisplayName("Address Line 4")]
[MaxLength(250)]
public string AddressLine4 { get; set; }
[DisplayName("Country")]
public ws_lookup_Country Country { get; set; }
[DisplayName("Parish")]
public ws_lookup_Parish Parish { get; set; }
[DisplayName("Home Telephone Number")]
[MaxLength(250)]
public string HomeTelephoneNumber { get; set; }
[DisplayName("Work Telephone Number")]
[MaxLength(250)]
public string WorkTelephoneNumber { get; set; }
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
[DisplayName("Date of Birth")]
public DateTime? DateOfBirth { get; set; }
[DisplayName("Gender")]
public ws_lookup_Gender Gender { get; set; }
[DisplayName("Loyalty Card")]
[MaxLength(50)]
public string LoyaltyCard { get; set; }
[DisplayName("Occupation")]
public ws_lookup_Occupation Occupation { get; set; }
}
誰能幫我找到了什麼我做錯了嗎?
爲什麼會出現在LINQ嘗試的國家代碼,而不是在原來的SQL? –