选择所有相关对象

时间:2011-11-09 15:05:58

标签: linq

我有一个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();

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();