2017-04-26 164 views
0

因爲有人告訴我,我需要一個新的問題:只返回某些列

我有這個疑問來自這個問題(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的列。我怎麼做?

+0

定義'一定columns'。 – Igor

回答

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();