This is a migrated thread and some comments may be shown as answers.

.distinct not working

2 Answers 132 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Markus
Top achievements
Rank 2
Markus asked on 03 Jun 2013, 09:51 AM
Good day all

I am trying to create a pulldown with all distinct cities from a table. I use this code below.

Any idea why the .Distinct() is not having any effect, and how I would have to create the statement?

(I am NOT getting an error. Simply all cities  not filtered distinct)

using (EntitiesModel dbContext = new EntitiesModel())
               {
                   List<T_user> FirmenNachOrt = (from a in dbContext.T_users
                                                 where a.User_benutzung == "Firma" && a.User_freischaltung == false
                                                 orderby a.User_ort
                                                     select  a).Distinct().ToList();
                   rddl_firmen_nach_ort.DataSource = FirmenNachOrt;
                   rddl_firmen_nach_ort.DataTextField = "user_ort";
                   rddl_firmen_nach_ort.DataValueField = "user_ort";
                   rddl_firmen_nach_ort.DataBind();
                   rddl_firmen_nach_ort.Items.Insert(0, new DropDownListItem("Nach Ort suchen"));
 
               }

Markus

2 Answers, 1 is accepted

Sort by
0
Viktor Zhivkov
Telerik team
answered on 03 Jun 2013, 01:27 PM
Hi Markus,

OpenAccess LINQ supports Distinct on the database server side only if you are doing it by a primitive property bound to a database column. If you are doing distinct by the whole entity object it will be done in-memory using .Net comparison rules.

If you want to get only the distinct values from T_users.User_ort you should modify your query slightly:
01.using (EntitiesModel dbContext = new EntitiesModel())
02.{
03.    List<string> FirmenNachOrt = (from a in dbContext.T_users 
04.                 where a.User_benutzung == "Firma" && a.User_freischaltung == false
05.                 orderby a.User_ort 
06.                 select  a.User_ort).Distinct().ToList();
07. 
08.    // TODO: bind the combobox properly to the new List<string>
09.}

If you need the whole T_users object, please implement a custom IEqualityComparer<T_users> and provide it as parameter of the Distinct method call. You should note that the filter will be applied in-memory and you will fetch some unnecessary data from the database.

If you need any further assistance do not hesitate to contact us again.

Regards,
Viktor Zhivkov
Telerik
OpenAccess Samples Kit boasts 50+ sample applications providing diverse real-life business solutions. Click to read more and see OpenAccess ORM in action.
0
Ru'a
Top achievements
Rank 1
answered on 31 Oct 2017, 01:27 PM
finally,,,,
thux  
Tags
LINQ (LINQ specific questions)
Asked by
Markus
Top achievements
Rank 2
Answers by
Viktor Zhivkov
Telerik team
Ru'a
Top achievements
Rank 1
Share this question
or