仅返回某些列

时间:2017-04-26 15:57:47

标签: c# linq

因为我被告知我需要提出一个新问题:

我从这个问题(Ranking Search Results With LINQ/.NET MVC)获得此查询:

var results = db.People
.Join(db.Menu, p => p.ID, m => m.PersonID, (p, m) => new { p = p, m = m })
.Join(db.Domain, m => m.m.DomainID, d => d.ID, (m, d) => new { m = m, d = d })
.Select(d => new
        {
            rank = searchTermArray.Any(x => d.m.p.p.Name.Contains(x)) ? 3 : searchTermArray.Any(x => d.m.p.p.Biography.Contains(x)) ? 2 : searchTermArray.Any(x => d.d.domain.Contains(x)) ? 1 : 0,
            m = d
        })
.Where(a => a.rank > 0)
.OrderByDescending(a => a.rank)
.Select(a => a.m).Distinct();

我需要从db.People返回排名和特定列。我该怎么做?

1 个答案:

答案 0 :(得分:0)

正如您在查询中所做的那样,新的{...}喜欢:

    var results = db.People
   .Join(db.Menu, p => p.ID, m => m.PersonID, (p, m) => new { p = p, m = m })
   .Join(db.Domain, m => m.m.DomainID, d => d.ID, (m, d) => new { m = m, d = d })
   .Select(d => new
    {
        rank = searchTermArray.Any(x => d.m.p.p.Name.Contains(x)) ? 3 :    searchTermArray.Any(x => d.m.p.p.Biography.Contains(x)) ? 2 : searchTermArray.Any  (x => d.d.domain.Contains(x)) ? 1 : 0,
        m = d
    })
  .Where(a => a.rank > 0)
  .OrderByDescending(a => a.rank)
  .Select(a =>  new {
  column1 = a.column1, 
  column2 = a.column2
  ....
  }).Distinct();