asp.net mvc - Linq to return true or false if a set contains distinct values -
if set returning contains 1 distinct value, return value (string). otherwise, return "multiple".
i have set ternary operator , know if can use linq sql determine whether or not set contains distinct value or multiple different values.
something following:
name = h.namelistings.select(a=>a.nameid).checkfordistinct() ? h.namelistings.select(a=>a.nameid).first() : "multiple"
i realize checkfordistinct() not linq method; using explain behavior looking for.
var name=h.namelistings.select(a=>a.nameid).distinct().count()==1 ? h.namelistings.select(a=>a.nameid).first(): "multiple";
here's 1 trip database solution, catching exception if there more 1 (may work in english versions -- don't know if exception messages can translated).
string result=""; try { result=h.namelistings.select(a=>a.nameid).distinct().single(); } catch(invalidoperationexception ex) { if (ex.message.contains("more one")) result="multiple"; else throw; }
Comments
Post a Comment