lambda 怎么做联合查询?

davidtsui davidtsui 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我有两个关联的实体,关系如下:



public class Answer
{
public int Id { get; set; }
public string Content{ get; set; }
public string Poster { get;set; }
public int QuestionId { get; set; }

public Question Question { get; set; }
}

public class Question
{
public int Id { get; set; }
public string Content { get; set; }
public List<Answer> Answers { get; set; }
}




现在我有三个问题:

1. 假如我知道了某个 Answer 的 id,怎么用 lambda 查询出对应的 Question?



int answerId = 2;
var result = context.Question.AsQueryable();

result = result.Where(p=>p.?); // 这里面该怎么写呢?



2. 假如我知道了某些 Answer 的 id,怎么用 lambda 查询出它们对应的 Question?



int[] answerIds = { 1, 2, 4, 6, 11 };
var result = context.Question.AsQueryable();

result = result.Where(p=>p.?); // 这里面该怎么写呢?




3. 如果知道 Answer 里的某个 Poster(回复人) 名称,怎么找出这个 Poster 回答过的所有 Question?



string poster = "John";
var result = context.Question.AsQueryable();

result = result.Where(p=>p.?); // 这里面该怎么写呢?



如上,谢谢各位~!
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
davidtsui
C# 111,128 成员 642,537 内容