Can you simplify this linq-to-entity groupBy query?
List<int> types = Hospitals.GroupBy(h => h.TypeId, (key, group) => group.First())
.Select(t=> y.TypeId).ToList();
Trying to get the distinct type ids from the List<Hospital>
objects. I'm not an ex开发者_StackOverflow社区pert in these linq querires and just want to know if there is a better way of doing this.
If all you want is the distinct TypeId fields, it's much simpler than that:
var types = Hospitals.Select(h => h.TypeId).Distinct();
Just a side-note on your original query: I often find that the method-style syntax of these operations is less readable than the query comprehension. It comes down to personal style, of course, but thus far I don't think I've ever written a GroupBy() or Join() in method syntax; to me this just flows better:
var types = from h in Hospitals
group h by h.TypeId into types
select types.Key;
精彩评论