我有一个Conversation
类,它对应于数据库中的多个表。
对话有一个Users
列表作为字段。我想根据Users
字段是否包含参数
我不确定该怎么做......
我知道这段代码不起作用甚至不能编译,但它应该让我知道我想要做什么:
public IHttpActionResult GetConversationForUser(String email)
{
IQueryable<Conversation> users = from d in db.Conversations
where d.Users == email; // I wish to something like **d.Users.Email == email** here but I am unsure how to express this correctly
if(users == null)
{
return NotFound();
}
return Ok();
}
答案 0 :(得分:3)
使用Any
扩展方法:
IQueryable<Conversation> users = from d in db.Conversations
where d.Users.Any(u=>u.Email== email)
select d;
答案 1 :(得分:1)
这样的事情应该有效:
public IHttpActionResult GetConversationForUser(String email)
{
IQueryable<Conversation> users = from d in db.Conversations
where d.Users.Any(u=>u.Email == email)
select d;
//Check if the query returns something not if the query is null, which won't happen
if(!users.Any())
{
return NotFound();
}
return Ok();
}
答案 2 :(得分:0)
我不确定我是否理解这个问题。如果我说得对,你可以写
var users = db.Conversations.Where(r => r.Email!= null || !r.Email.Equals(string.Empty))