c# - How to make Distinct... More distinct? -


ok, aside silly title. have query pulling distinct values including departmentid column instead of departmentname column distinct. makes me causes duplicates.

viewbag.departmentlist = docs.select(m => new selectlistitem             {                 value = sqlfunctions.stringconvert((double)m.departmentid).trim(),                 text = m.departmentname             })             .distinct(); //  fill viewbag unique list of 'department's table. 

here docs has inside it:

image enter image description here

if helps @ all, departmentid primary key. departmentid 1 point same name , on.

you can use iequalitycomparer

class mycomparer<t> : iequalitycomparer<t> t : selectlistitem {     public bool equals(t x, t y)     {         return x.value == y.value ;     }      public int gethashcode(t obj)     {         return obj.id.gethashcode();     } } 

then code

viewbag.departmentlist = docs.select(m => new selectlistitem         {             value = sqlfunctions.stringconvert((double)m.departmentid).trim(),             text = m.departmentname         })         .distinct(new mycomparer<selectlistitem>()).tolist(); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -