Linq 选择所有相关对象
我有一个linq查询,其中我试图返回与所有其他Linq 选择所有相关对象,linq,Linq,我有一个linq查询,其中我试图返回与所有其他WebObjects相关的所有MlaArticles,但我得到了一个错误:指定的类型成员“RelatedWebObjectIds”在linq to实体中不受支持。仅支持初始值设定项、实体成员和实体导航属性。 这是模型 public abstract class WebObject : IValidatableObject { public WebObject() { this.Id = Guid.NewGuid();
WebObjects
相关的所有MlaArticles
,但我得到了一个错误:指定的类型成员“RelatedWebObjectIds”在linq to实体中不受支持。仅支持初始值设定项、实体成员和实体导航属性。
这是模型
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; }
}
您的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();
List assignedWebObjects=(来自db.MlaArticles中的e)
哪里
(来自db.WebObjects中的w)
从r到w.RelatedWebObjects
其中w.Id==Id
选择r.Id)。包含(e.Id)
选择e).OrderBy(x=>x.Title.ToList();
列出分配的WebObjects=(来自db.MlaArticles中的e
哪里
(来自db.WebObjects中的w)
从r到w.RelatedWebObjects
其中w.Id==Id
选择r.relatedWebObjectId).Any(i=>i==e.Id)
选择e).OrderBy(x=>x.Title.ToList();
是的,有。但是当我引用RelatedWebObjects时,我得到一个Contains错误:WebObject不包含“Contains”的定义,并且最佳扩展方法重载。。。有一些无效参数。
您确定使用的是相同的查询吗?也许您忘记将select r
替换为select r.Id
?它需要是r.relatedWebObjectId,但仍然会引发相同的错误。我将把我的新查询放在上面…好吧,使用r.relatedWebObjectId或r.Id没有相同的含义。。。但我不知道你的模型,所以很难判断哪个更正确。你应该在问题中发布你的模型这只是冰山一角。我将尝试在上面进行总结。运算符==不能应用于'IList'和'System.Guid'类型的操作数。
。e、 Id是一个Guid,我是一个Guid列表。
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();
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();
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();