开发者

LINQ to SQL select all fields in a table but with a distinct column

I need to return a list of counties, but I n开发者_JS百科eed to filter out duplicate phone code values. For some reason I'm having trouble with the syntax. Can anyone show me how to do this? Should I be using the group by instead?


Group by would work if you need the actual entity.

var query = db.Counties.GroupBy( c => new { c.CountyName, c.PhoneCode } )
                       .Select( g => g.FirstOrDefault() );

Or if you are constructing it for a view model and only need the data, you could use Distinct. The following creates an anonymous type that could be used to populate the model.

var query = db.Counties.Select( c => new { c.CountyName, c.PhoneCode } )
                       .Distinct();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜