林克命令几句话

时间:2015-09-29 15:18:20

标签: c# linq

如果searchWords包含2次相同的单词,我如何订购此列表?

var resultList = from c in context.Category
    join q in context.Question on c.CategoryId equals q.CategoryId
    join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
    from a2 in QuestAnsw.DefaultIfEmpty()
    where q.CustomerId == customerId 
    && (searchWords.Any(w => a2.Text.Contains(w))
    || searchWords.Any(w => c.Text.Contains(w))
    || searchWords.Any(w => q.Text.Contains(w)))
    select new { Category = c, Question = q };

我可以执行下面的if语句

 && (searchWords.Any(w => a2.Text.Contains(w))
    || searchWords.Any(w => c.Text.Contains(w))
    || searchWords.Any(w => q.Text.Contains(w))) if(true) order
 select new { Category = c, Question = q };

我的最后一次更改(它有效,但不适用于子类别)

  var resultList = from c in context.Category
      join q in context.Question on c.CategoryId equals q.CategoryId
      join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
      from a2 in QuestAnsw.DefaultIfEmpty()
      where q.CustomerId == customerId
      && (searchWords.Any(w => a2.Text.Contains(w) || c.Text.Contains(w) || q.Text.Contains(w)))
   orderby
     (searchWords.All(w => a2.Text.Contains(w))
      && searchWords.All(w => c.Text.Contains(w))
      || searchWords.All(w => q.Text.Contains(w)))
   descending 
      select new { Category = c, Question = q };

1 个答案:

答案 0 :(得分:3)

如果您想在顶部(或底部)显示匹配的项目,您可以根据自己的条件设置顺序:

    public ActionResult Create(int id)
    {
        ViewBag.GroupId = id;
        Debug.WriteLine("DEBUG: "+id);
        return View();
    }

此外,如果您需要在匹配项目之间设置顺序,您可以添加其他排序表达式来订购匹配项目:

var resultList = from c in context.Category
    join q in context.Question on c.CategoryId equals q.CategoryId
    join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
    from a2 in QuestAnsw.DefaultIfEmpty()
    where q.CustomerId == customerId 
    orderby 
        (searchWords.Any(w => a2.Text.Contains(w))
        || searchWords.Any(w => c.Text.Contains(w))
        || searchWords.Any(w => q.Text.Contains(w))) 
    descending
    select new { Category = c, Question = q };