开发者

MVC3/LINQ/EF4.1 selecting distinct col values from a result set?

How can I select a list of column values from a result set (as distinct) and put into a list?

class T {int id; string name;}

-- Controller...

var query = 开发者_StackOverflow社区@"exec someStoredProc";
IEnumerable<T> bb =
 db2.Database.SqlQuery<T>(query);    

// Something like???:
List<string> Names = bb.SelectDistinct("name");  // returns distinct list of names from result set


Since you just need the distinct list of names, you can project to the name property and the just use Distinct() :

List<string> Names = bb.Select( x=> x.name)
                       .Distinct()
                       .ToList();

This requires that you make the name property public, also I would rethink your class name T, how about CustomerName (or whatever else is expressive enough so you know what it means) ?

public class CustomerName 
{
  public int id{get;set;}
  public string name {get;set;}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜