我被困在ASP.NET MVC 3应用程序的某个地方。这是我得到的错误:
不支持指定的类型成员'AccommPropertyTags' LINQ to Entities。仅初始化程序,实体成员和实体 支持导航属性。
我已经找到了如何在以下文章中解决这个问题:
Only initializers, entity members, and entity navigation properties are supported
但我的有点奇怪。
这是我的实体的部分类之一:
[MetadataType(typeof(AccommPropertyWebDetail.MetaData))]
public partial class AccommPropertyWebDetail {
public virtual ICollection<string> AccommPropertyTags {
get {
return Helpers.AccommPropertyTag.CreateStringListFromString(this.PropertyTags);
}
}
private class MetaData {
[Required, StringLength(50)]
public string Category { get; set; }
}
}
如上所示,AccommPropertyTags
属性为ICollection<string>
的typeof。我在控制器中尝试的内容如下:
public ViewResult Tag(string tag) {
var _rawTag = HttpUtility.UrlDecode(tag);
ViewBag.Tag = _rawTag;
var model = _accommpropertyrepo.GetAllAccommPropertiesFullWebIgnoringApprovalStatus();
model = model.Where(
x => x.AccommPropertyTags.Any(
y => y == _rawTag
)
);
return View(model);
}
由于我在那里使用Any
这一事实,实体正在尝试将我的AccommPropertyTags
属性转换为SQL并且因为它不是表模式的一部分而无法使用。
我真的被困在这里还是有一种很酷的方式来打败这个恼人的错误?
答案 0 :(得分:10)
您的问题与您关联的问题类似。在使用model.ToList()
之前致电Where
。这将强制EF实现实体,然后在内存中应用剩余的过滤。
public ViewResult Tag(string tag) {
var _rawTag = HttpUtility.UrlDecode(tag);
ViewBag.Tag = _rawTag;
var model = _accommpropertyrepo.GetAllAccommPropertiesFullWebIgnoringApprovalStatus();
var result = model.ToList().Where(
x => x.AccommPropertyTags.Any(
y => y == _rawTag
)
);
return View(result);
}