本文介绍了LINQ&实体框架 - List.Contains不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用.NET 4在带有.NET 4的MVC 4 WebAPI中使用LINQ和Entity Framework 6过滤数据。
问题已经结束了:
I'm trying to filter data with LINQ and Entity Framework 6 in MVC 4 WebAPI with .NET 4.
The issue is over here:
public object Get(int id)
{
SiscalContext sys = new SiscalContext();
if (!sys.FileExist(id))
throw new Exceptions.BadRequest.FIDNoExist();
User LoggedUser = sys.AuthUserInfo();
File file = sys.Files.Where(itm => itm.ID == id).FirstOrDefault();
if (file.Owner != LoggedUser.ID && (file.Permissions == Permissions.OnlyMe || (file.Permissions == Permissions.Specific && !file.Group.Contains(LoggedUser.ID))))
throw new Exceptions.Forbidden.NotAllowed();
return file;
}
具体在:
Specifically in:
!file.Group.Contains(LoggedUser.ID)
如果超出此限制,则会出现下一个错误:
LINQ to Entities不支持指定的类型成员Group。仅支持初始值设定项,实体成员和实体导航属性。
File的模型是下一个:
When this is excecuted the next error appears:The specified type member 'Group' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
The model of File is the next:
public enum Permissions { OnlyMe, Specific, Public };
public enum FileType { Folder, File };
public class File
{
public int ID { get; set; }
public string Name { get; set; }
public int Directory { get; set; }
public int Owner { get; set; }
public Permissions Permissions { get; set; }
public virtual List<int> Group
{
get
{ return string.IsNullOrEmpty(this.GroupJSON) ? new List<int>() : JsonConvert.DeserializeObject<List<int>>(this.GroupJSON); }
set
{ GroupJSON = JsonConvert.SerializeObject(value); }
}
[JsonIgnore]
public string GroupJSON { get; private set; }
public FileType Type { get; set; }
}
我可以做些什么来继续使用相同的模型?
What I can do to keep using the same model?
推荐答案
这篇关于LINQ&实体框架 - List.Contains不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!