這是我的數據模型的例子:在多對多的關係中,你如何防止往返? - 實體框架
public class House : Record
{
public string HouseNumber { get; set; }
public string StreetName { get; set; }
public int ZipCode { get; set; }
public virtual ICollection<Street> Streets { get; set; }
}
public class Street
{
public int StreetId { get; set; }
public string StreetName { get; set; }
public virtual ICollection<House> HouseUnit { get; set; }
public virtual ICollection<House> HouseUnitParallel { get; set; }
}
我想只獲得StreetId和StreetName從街道(不含HouseUnit和HouseUnitParallel)每家的紀錄。
var entities = context.Houses.Include(r => r.Streets.Select(s => new
{
StreetId = s.StreetId,
StreetName = s.StreetName
}));
此代碼引發以下異常:
The Include path expression must refer to a navigation property defined on the type.
Use dotted paths for reference navigation properties and the Select operator
for collection navigation properties.
每家記錄,我想排除我的街道物業內的HouseUnit和HouseUnitParallel集合(因爲它捎帶了一堆房子來我的房子紀錄)。
我能做些什麼來解決這個問題?
然後你會得到只有街道回來作爲匿名類型忽略所有的房屋屬性,擊敗連接的目的。 – eugenekgn
@eugenekgn - OP似乎只想要屬性StreetId和StreetName(我怎麼知道的?他說「我想只獲得StreetId和StreetName」......另外,他的嘗試解決方案只返回了「Select ')。他還出現了一個匿名類型的內容(同樣,因爲他的解決方案在'Select'中返回了一個匿名類型)。 –
這可能是我誤解了OP想要做的 - 我會編輯我的答案。 –