我很努力地将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? –