var users = context.Users
.Where(u => u.Events.Any(q => q.EventName == eventName))
.Select(m => m.FirstName + m.LastName).ToList();
更新,添加测试相关类;
public class User
{
public List<Event> Events { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Event
{
public string EventName { get; set; }
}
和代码;
string eventName = "a";
List<User> t = new List<User>();
Event event1 = new Event() { EventName = "a" };
Event event2 = new Event() { EventName = "b" };
Event event3 = new Event() { EventName = "c" };
User user1 = new User() { FirstName = "x", LastName = "y" };
User user2 = new User() { FirstName = "x1", LastName = "y1" };
User user3 = new User() { FirstName = "x2", LastName = "y2" };
user1.Events = new List<Event>();
user2.Events = new List<Event>();
user3.Events = new List<Event>();
user1.Events.Add(event1);
user1.Events.Add(event2);
user2.Events.Add(event2);
user2.Events.Add(event3);
user3.Events.Add(event1);
user3.Events.Add(event3);
t.Add(user1);
t.Add(user2);
t.Add(user3);
var bb = t.Where(u => u.Events.Any(q => q.EventName == eventName)).Select(m => m.FirstName + m.LastName).ToList();