2011-11-09 73 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

相关问题