【发布时间】:2015-08-25 19:22:57
【问题描述】:
我需要将此 SQL 查询转换为 c# 中的 LINQ。我将不胜感激您可以提供的任何帮助或指导。 (我使用的是实体框架 6)。
SELECT f.FacId
,sli.SitLocIntId
,ei.EnvIntId
,p.PhoId
FROM Fac AS f
join SitLocInt AS sli on f.FacId = sli.FacId
join EnvInt AS ei on sli.EnvIntId = ei.EnvIntId
join EnvIntTyp AS eit on ei.EnvIntTypId = eit.EnvIntTypId
left outer join Aff AS a on ei.EnvIntId = a.EnvIntId
left outer join AffCon AS ac on a.AffId = ac.AffId
left outer join Con AS c on ac.ConId = c.ConId
left outer join Pho AS p on c.ConId = p.ConId
WHERE EnvIntTyp = 'Fleet'
我一直在研究组连接语法,但是对于这么多表、大量左外连接和 where 子句,我没有取得太大进展。到目前为止我有
var testQuery =
from f in _CEMDbContext.setFac
join sli in _CEMDbContext.setSitLocInt on f.FacId equals sli.FacId
join ei in _CEMDbContext.setEnvInt on sli.EnvIntId equals ei.EnvIntId
join eit in _CEMDbContext.setEnvIntTyp on ei.EnvIntTypId equals eit.EnvIntTypId into eiGroup
from item in eiGroup.DefaultIfEmpty().Where(e => e.EnvIntTyp == "Fleet")
select new BusinessParticipant
{
fac = f,
sitLocInt = sli,
envInt = ei,
// pho = p, // not working... Phone number from table Pho.
};
var TestList = testQuery.ToList();
正在获取 EnvIntTyp“Fleet”及其所有相关数据,包括 Fac、SitLocInt 和 EnvInt。但是当我尝试从可能存在或不存在的 Pho 表中获取电话号码时,我遇到了麻烦。有没有一种很好的方法将所有左外连接链接在一起?感谢您的建议或指导!
编辑 我不仅需要我在 SQL 查询中编写的 Id,还需要整个对象。另外,我应该提到 EnvIntTyp 是同名表的成员。
编辑 2 以下是相关实体的(部分)。 Fac 是 Facility 的缩写。 SitLocInt 是 SiteLocationInterest 的缩写。 EnvInt 代表环境利益。 Aff 是从属关系。 Con 用于联系。 Pho 代表电话。
public partial class Facility
{
public Facility()
{
this.SiteLocationInterests = new List<SiteLocationInterest>();
}
public int FacilityId { get; set; }
public string FacilityIdentifier { get; set; }
public string FacilityName { get; set; }
public int SiteTypeId { get; set; }
public string FacilityDescription { get; set; }
[ForeignKey("GeographicFeature")]
public Nullable<int> GeographicFeatureId { get; set; }
[Display(Name = "resAddress", ResourceType = typeof(CEMResource))]
public string AddressLine1 { get; set; }
[Display(Name = "resAddressLine2", ResourceType = typeof(CEMResource))]
public string AddressLine2 { get; set; }
public string City { get; set; }
[Display(Name = "resState", ResourceType = typeof(CEMResource))]
public string StateCode { get; set; }
[Display(Name = "resZip", ResourceType = typeof(CEMResource))]
public string AddressPostalCode { get; set; }
public string CountyName { get; set; }
public virtual GeographicFeature GeographicFeature { get; set; }
public virtual ICollection<SiteLocationInterest> SiteLocationInterests { get; set; }
}
public partial class SiteLocationInterest
{
public int SiteLocationInterestId { get; set; }
[ForeignKey("Facility")]
public Nullable<int> FacilityId { get; set; }
[ForeignKey("EnvironmentalInterest")]
public Nullable<int> EnvironmentalInterestId { get; set; }
public Nullable<int> EventId { get; set; }
[ForeignKey("GeographicFeature")]
public Nullable<int> GeographicFeatureId { get; set; }
public System.DateTime CreateDate { get; set; }
public string CreateBy { get; set; }
public System.DateTime LastUpdateDate { get; set; }
public string LastUpdateBy { get; set; }
public virtual EnvironmentalInterest EnvironmentalInterest { get; set; }
public virtual Facility Facility { get; set; }
public virtual GeographicFeature GeographicFeature { get; set; }
}
public partial class EnvironmentalInterest
{
public EnvironmentalInterest()
{
this.Affiliations = new List<Affiliation>();
this.SiteLocationInterests = new List<SiteLocationInterest>();
}
public int EnvironmentalInterestId { get; set; }
public string EnvironmentalInterestIdentifier { get; set; }
public string EnvironmentalInterestFacilityIdentifier { get; set; }
public string FacilityIdentifier { get; set; }
public string EnvironmentalInterestName { get; set; }
[ForeignKey("EnvironmentalInterestType")]
public int EnvironmentalInterestTypeId { get; set; }
public Nullable<int> EnvironmentalInterestSubTypeId { get; set; }
public string EnvironmentalInterestDescription { get; set; }
public virtual ICollection<Affiliation> Affiliations { get; set; }
public virtual EnvironmentalInterestType EnvironmentalInterestType { get; set; }
public virtual ICollection<SiteLocationInterest> SiteLocationInterests { get; set; }
}
public partial class Affiliation
{
public Affiliation()
{
this.AffiliationContacts = new List<AffiliationContact>();
}
public int AffiliationId { get; set; }
public string AffiliationIdentifier { get; set; }
public string AffiliationName { get; set; }
[ForeignKey("Entity")]
public Nullable<int> EntityId { get; set; }
[ForeignKey("EnvironmentalInterest")]
public Nullable<int> EnvironmentalInterestId { get; set; }
public int AffiliationTypeId { get; set; }
public int StatusTypeId { get; set; }
public virtual EnvironmentalInterest EnvironmentalInterest { get; set; }
public virtual ICollection<AffiliationContact> AffiliationContacts { get; set; }
}
public partial class AffiliationContact
{
public int AffiliationContactId { get; set; }
public int AffiliationId { get; set; }
public int ContactId { get; set; }
public virtual Affiliation Affiliation { get; set; }
public virtual Contact Contact { get; set; }
}
public partial class Contact
{
public Contact()
{
this.AffiliationContacts = new List<AffiliationContact>();
this.Phones = new List<Phone>();
}
public int ContactId { get; set; }
public string ContactIdentifier { get; set; }
public int EntityId { get; set; }
public Nullable<int> MailAddressId { get; set; }
public int ContactTypeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public virtual ICollection<AffiliationContact> AffiliationContacts { get; set; }
public virtual Entity Entity { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
public partial class Phone
{
public int PhoneId { get; set; }
public int ContactId { get; set; }
public int ContactTypeId { get; set; }
public string PhoneNumber { get; set; }
public string PhoneExtensionNumber { get; set; }
public virtual Contact Contact { get; set; }
public virtual ContactType ContactType { get; set; }
}
【问题讨论】:
-
这些连接是基于外键的吗?如果是这样,那么 EF 应该已经创建了您可以使用的导航属性。
-
停下来。为什么,哦,为什么人们一直在跳槽,付出巨大的努力将完美的代码转换成另一种语言的基本相同的代码?您的用户并不关心您的查询是使用 C# 还是 SQL...
-
原SQL中的第三个
JOIN似乎是不必要的。 -
这就是我走捷径所得到的。我真的需要整个对象,而不仅仅是我在 SQL 中写的 Id。
-
您可能可以执行
where f.SetSitLocInt.SetEnvInt.SetEnvIntTyp.EnvIntType == "Fleet"之类的操作并避免连接,但我们需要查看相关实体才能知道您必须使用哪些导航属性。
标签: c# linq entity-framework left-join