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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -