Search code examples
c#asp.net-mvclinq-to-entities

Selecting based on string in a list C# LINQ


So I have a class named event which contains a List ListofInvites, It has email addresses of different user(another class which has a emailid property), now I want to select all the events based on whether the listofinvites string contains that specific email

I have tried this but i'm getting an error

var userEmailID = this.User.Identity.GetUserName();


var events = this.dataBase.Events
       .OrderBy(e => e.DateAndTime)
       .Where(e => e.listOfInvites.Contains(userEmailID))
       .Select(EventViewModel.ViewModel);

Yeah sorry I was in a hurry last night when i posted this

So I have this event class

public class Event
{
    public Event()
    {
        this.IsPublic = true;
        this.DateAndTime = DateTime.Now;
    }
    [Key]
    public int EventID { get; set; }

    [Required]
    [MaxLength(200)]
    public string Title { get; set; }

    [Required]
    public DateTime DateAndTime { get; set; }

    public string AuthorId { get; set; }
    public int Duration { get; set; }
    public string Description { get; set; }
    public string OtherDetails { get; set; }
    [MaxLength(200)]
    public string Location { get; set; }
    public bool IsPublic { get; set; }
    public IEnumerable<string> listOfInvites { get; set; }

    public string  InviteList { get; set; }
}
}

the error i'm getting is

System.NotSupportedException: 'The specified type member 'listOfInvites' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.'


Solution

  • In the Event entity, you are declaring a IEnumerable<string> property (listOfInvites) while list of primitive types is not supported in Entity Framework and since this property is not mapped to a database column you cannot use it in a where expression.
    You need to create another entity (therefore table) for ListOfInvites with a foreign key to the Event entity.