2011-11-09 71 views
3

我有一個LINQ查詢在那裏我試圖返回所有MlaArticles這涉及到所有其他WebObjects但我得到的錯誤:The specified type member 'RelatedWebObjectIds' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.選擇所有相關的對象

這裏的型號......

public abstract class WebObject : IValidatableObject 
{ 
    public WebObject() 
    { 
     this.Id = Guid.NewGuid(); 
     RelatedTags = new List<Tag>(); 
     RelatedWebObjects = new List<WebObject>(); 
    } 

    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public Guid Id { get; set; } 
    public virtual ICollection<WebObject> RelatedWebObjects { get; set; } 
    public IList<Guid> RelatedWebObjectIds { get; set; } 
} 

感謝您的幫助......

List<MlaArticle> assignedWebObjects = (from e in db.MlaArticles 
             where 
              (from w in db.WebObjects 
              from r in w.RelatedWebObjectIds 
              where w.Id == id 
              select r).Contains(e.Id) 
             select e).OrderBy(x => x.Title).ToList(); 

新查詢。產生不同的錯誤:WebObject does not contain a definition for 'Contains' and the best extension method overload ... has some invalid arguments.

List<MlaArticle> assignedWebObjects = (from e in db.MlaArticles 
             where 
              (from w in db.WebObjects 
              from r in w.RelatedWebObjects 
              where w.Id == id 
              select r.RelatedWebObjectIds).Contains(e.Id) 
             select e).OrderBy(x => x.Title).ToList(); 

回答

0

有沒有在您的MlaArticle實體RelatedWebObjects導航屬性?如果有,你能做到這一點,而不是:

List<MlaArticle> assignedWebObjects = (from e in db.MlaArticles 
             where 
              (from w in db.WebObjects 
              from r in w.RelatedWebObjects 
              where w.Id == id 
              select r.Id).Contains(e.Id) 
             select e).OrderBy(x => x.Title).ToList(); 
+0

是的,有。但是當我引用RelatedWebObjects時,我得到一個Contains的錯誤:'WebObject不包含'Contains'的定義和最好的擴展方法重載...有一些無效的參數。' – bflemi3

+0

你確定你使用的是相同的查詢嗎?也許你忘了用'select r.Id'替換'select r'? –

+0

它需要是r.RelatedWebObjectIds,但仍會拋出相同的錯誤。我會把我的新查詢放在上面...... – bflemi3

0
List<MlaArticle> assignedWebObjects = (from e in db.MlaArticles 
             where 
              (from w in db.WebObjects 
              from r in w.RelatedWebObjects 
              where w.Id == id 
              select r.RelatedWebObjectIds).Any(i => i == e.Id) 
             select e).OrderBy(x => x.Title).ToList(); 
+0

運算符==不能應用於'IList '和'System.Guid'類型的操作數。 e.Id是Guid,我會列出Guid。 – bflemi3

相關問題