Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to filter data with LINQ and Entity Framework 6 in MVC 4 WebAPI with .NET 4.

The issue is over here:

C#
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:

C#
!file.Group.Contains(LoggedUser.ID)

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:

C#
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?
Posted
Comments
Maciej Los 1-Jan-15 11:28am    
Group is reserved word! use: public virtual List<int> Groupp instead
BillWoodruff 1-Jan-15 12:12pm    
If that fixes the OP's problem, I think you should post this as a solution so it can be righteously up-voted.
Maciej Los 1-Jan-15 12:15pm    
The problem is that i'm not sure if that fixes OP's problem ;)
All The Best in New Year, Bill!
TANicox 1-Jan-15 20:14pm    
Nope, it still doesn't working. I think that the problem comes from the compatibility of Lists in Entity-Framework and LINQ (At least that says the error).

I don't how to address the problem, making something to add support of Enumerables in EF
Maciej Los 2-Jan-15 2:15am    
Please, see my answer. Happy New Year!

1 solution

Thank you for clarification.

I think you need to create public member of File class: List<int> JsonGroup (instead of string JsonGroup), becasue it is initialized via Json.DeserializeObject method. Then you'll be able to use Contains method ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900