简体   繁体   English

基于列表中的字符串选择C#LINQ

[英]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 所以我有一个名为event的类,它包含一个ListofInvites列表,它具有不同用户的电子邮件地址(另一个类具有emailid属性),现在我想根据listofinvites字符串是否包含该特定电子邮件来选择所有事件

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.'

Hope the below code will solve your issue. 希望以下代码能解决您的问题。

var events = dataBase.Events.Where(p => p.listOfInvites.Contains(userEmailID)).ToList(); var events = dataBase.Events.Where(p => p.listOfInvites.Contains(userEmailID))。ToList();

It would help to see your classes, but I'm guessing you have a list of some class named Event in your database, each of which has a List<Invite> property, and then an EmailId string property on each one of those Invite instances. 这将有助于查看您的类,但是我想您在数据库中有一个名为Event类的列表,每个类都有一个List<Invite>属性,然后在每个这些Invite实例上都有一个EmailId string属性。 If that's the case, since your EmailId is not a property in the Event itself, you'll have to iterate through each event's List<Invite> to get the EmailId associated with it. 如果是这种情况,由于您的EmailId不是Event本身的属性,因此您必须遍历每个事件的List<Invite>以获得EmailId关联的EmailId The relations I'm guessing are: Many events in your database; 我猜测的关系是:数据库中的许多事件; many invites on each Event ; 每个Event 许多邀请; and one EmailID on each Invite . 每个Invite上有一个 EmailID

Following this assumptions, I would do this: 按照这个假设,我会这样做:

var events = this.database.Events.Where(e => e.ListOfInvites.Any(i => i.EmailId.Contains(userEmailId))).ToList();

So, from each of the events on the (assummed) List<Event> on your database, we'll search through every invite in it's ListOfInvites property (that's the e => e.ListOfInvites part), looking for any invite where it's EmailId matches the one we passed, ie: i => i.EmailId.Contains(userEmailId) . 因此,从数据库中(假定的) List<Event>上的每个List<Event> ,我们将搜索它的ListOfInvites属性(即e => e.ListOfInvites部分)中的每个邀请,以查找在EmailId任何邀请与我们传递的匹配,即: i => i.EmailId.Contains(userEmailId) This var will then become a List<Event> representing all the events to which our user is invited. 然后,该var将成为List<Event>代表要邀请我们的用户参加的所有事件。

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. Event实体中,您要声明IEnumerable<string>属性( listOfInvites ),而在Entity Framework中不支持基本类型的列表,并且由于此属性未映射到数据库列,因此您不能在where表达式中使用它。
You need to create another entity (therefore table) for ListOfInvites with a foreign key to the Event entity. 您需要使用Event实体的外键为ListOfInvites创建另一个实体(因此表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM