Linq过滤问题

时间:2011-08-16 23:15:26

标签: .net linq

伙计们其实我想做这样的查询:

var firstTakeIssues = _db.Query<Issue>().OrderByDescending(i => i.CreatedDate)
                          .Take(take)
                          .Having(i => i.Project.ProjectID in IEnumerable<int>)
                          .Select(i => new {
                                  IssueId = i.IssueID,
                                  State = (StateEnum)i.State,
                                  ProjectName = i.Project.Name,
                                  Priority = (PriorityEnum)i.Priority,
                                  CreatedAt = i.CreatedDate,
                                  PostedBy = i.Client.Name
                              }).ToList();

我知道这不行,但我想做的是:

  1. 按集合中的项目搜索
  2. 按创建日期排序
  3. 只选择一组数据
  4. 获取列的子集
  5. 我真正想要指出的是,我不知道如何使用having子句进行查询。

1 个答案:

答案 0 :(得分:0)

要模仿您的“拥有”条款,我相信您应该能够使用Where以及Contains

var projectIds = new int[] { 1, 2, 4, 6 };
var firstTakeIssues = _db.Query<Issue>()
                         .OrderByDescending(i => i.CreatedDate)
                         .Take(take)
                         .Where(i => projectIds.Contains(i.Project.ProjectID))
                         .Select(i => new {
                              IssueId = i.IssueID,
                              State = (StateEnum)i.State,
                              ProjectName = i.Project.Name,
                              Priority = (PriorityEnum)i.Priority,
                              CreatedAt = i.CreatedDate,
                              PostedBy = i.Client.Name
                          }).ToList();