distinct - Names disintct in linq -


i try select name there names repeat want select these through distinct how done in linq

string data2 = "["; t1 db = new t1(); var te = db.tblvni;  foreach (var c in te) {     data2 += "'" + c.name + "',"; }  data2 = data2.substring(0, data2.length - 1); data2 += "]"; 

this return me this

['dpsb','mv','psb','','','','psb','psb','','psb','psb','mv','','','mv','psb','','mv','mv','mv','','psb','mv',

where dont want repeat these names

any solution

you replace

foreach (var c in te) {     data2 += "'" + c.name + "',"; } 

by following snippet.

foreach (var c in te.select( x => x.name ).distinct()) {     data2 += "'" + c + "',"; } 

you use existing facilities , rewrite entire part

t1 db = new t1(); var te = db.tblvni;  string data2     = string.format("[{0}]",           string.join( ",", te.te.select( x => x.name ).distinct() ) ); 

which not require manual removal of terminal comma. however, in total, indeed json should not generated manually, there existing tools that, this one.


Comments

Popular posts from this blog

iis - ASP.Net Core CreatedAtAction in HttpPost action returns 201 but entire request ends with 500 -

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -