Linq多对多查询
我需要帮助来查询这三个表。RentCommunityFeature和RentPropertyFeature与RentUnitListing具有多对多关系。我的问题是我无法查询这三个表。我想要的是所有具有特定功能的租赁清单。例如,如果RentCommunityFeature有一个“pool”和RentPropertyFeature有一个“parking”,我想要RentUnitListing中所有有“pool”和“parking”的记录。如果没有停车,则结果应仅显示“池” 我尝试了下面的查询,但结果不正确。当myCommunityFeatureId或myPropertyFeatureId为-1时,它显示重复的结果。如果它们在DB中为空,我将它们初始化为-1 任何帮助都将不胜感激Linq多对多查询,linq,entity-framework,c#-4.0,ef-code-first,many-to-many,Linq,Entity Framework,C# 4.0,Ef Code First,Many To Many,我需要帮助来查询这三个表。RentCommunityFeature和RentPropertyFeature与RentUnitListing具有多对多关系。我的问题是我无法查询这三个表。我想要的是所有具有特定功能的租赁清单。例如,如果RentCommunityFeature有一个“pool”和RentPropertyFeature有一个“parking”,我想要RentUnitListing中所有有“pool”和“parking”的记录。如果没有停车,则结果应仅显示“池” 我尝试了下面的查询,但结
var AllAds = from r in _db.RentUnitListings
from cf in r.RentCommunityFeatures
from pf in r.RentPropertyFeatures
where (myCommunityFeatureId > 0) ? (cf.RentCommunityFeatureID == myCommunityFeatureId && cf.RentUnitListings.) : (myCommunityFeatureId == -1)
where (myPropertyFeatureId > 0) ? (pf.RentPropertyFeatureID == myPropertyFeatureId) : (myPropertyFeatureId == -1)
select r;
public partial class RentCommunityFeature
{
public int RentCommunityFeatureID { get; set; }
public string RentCommunityFeatureDescription { get; set; }
public virtual ICollection<RentUnitListing> RentUnitListings { get; set; }
}
public partial class RentPropertyFeature
{
public int RentPropertyFeatureID { get; set; }
public string RentPropertyFeatureDescription { get; set; }
public virtual ICollection<RentUnitListing> RentUnitListings { get; set; }
}
public partial class RentUnitListing
{
public Guid RentUnitListingID { get; set; }
public string RentUnitListingShortDescription { get; set; }
public virtual ICollection<RentCommunityFeature> RentCommunityFeatures { get; set; }
public virtual ICollection<RentPropertyFeature> RentPropertyFeatures { get; set; }
}
var AllAds=from r in _db.RentUnitListings
来自r.RentCommunityFeatures中的cf
来自r.RentPropertyFeatures中的pf
其中(myCommunityFeatureId>0)?(cf.RentCommunityFeatureID==myCommunityFeatureId&&cf.RentUnitListings。):(myCommunityFeatureId==1)
其中(myPropertyFeatureId>0)?(pf.RentPropertyFeatureID==myPropertyFeatureId):(myPropertyFeatureId==1)
选择r;
公共部分类RentCommunityFeature
{
公共int RentCommunityFeatureID{get;set;}
公共字符串RentCommunityFeatureDescription{get;set;}
公共虚拟ICollection RentUnitListings{get;set;}
}
公共部分类出租属性功能
{
public int-RentPropertyFeatureID{get;set;}
公共字符串RentPropertyFeatureDescription{get;set;}
公共虚拟ICollection RentUnitListings{get;set;}
}
公共部分类出租单元列表
{
公共Guid RentUnitListingID{get;set;}
公共字符串RentUnitListingShortDescription{get;set;}
公共虚拟ICollection RentCommunityFeatures{get;set;}
公共虚拟ICollection RentPropertyFeatures{get;set;}
}
这意味着:返回至少有一个(Any
)RentCommunityFeature
带有myCommunityFeatureId
或至少有一个(Any
)RentPropertyFeature
带有myPropertyFeatureId
的列表。“或”不是排他性的,因此返回的列表可能有一个没有“停车”功能的“池”,或者有一个没有“池”功能的“停车”,或者两者都有。在任何情况下,返回的列表可能除了“Pool”或“Parking”之外还有很多其他特性
这意味着:返回至少有一个(Any
)RentCommunityFeature
带有myCommunityFeatureId
或至少有一个(Any
)RentPropertyFeature
带有myPropertyFeatureId
的列表。“或”不是排他性的,因此返回的列表可能有一个没有“停车”功能的“池”,或者有一个没有“池”功能的“停车”,或者两者都有。在任何情况下,返回的列表可能除了“Pool”或“Parking”之外还有很多其他特性
var listings = _db.RentUnitListings
.Where(rul => rul.RentCommunityFeatures
.Any(rcf => rcf.RentCommunityFeatureID == myCommunityFeatureId)
|| rul.RentPropertyFeatures
.Any(rpf => rpf.RentPropertyFeatureID == myPropertyFeatureId))
.ToList();