Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using EntitySpaces 2012 architecture which provides ORM mapping and built-in methods to write your SQL query using it's libraries (just like LINQ to SQL). The problem is, I cannot select distinct rows from a table using EntitySpaces Select Query. Currently, I am doing it as follows

C#
DataTable dt = new DataTable();
ActualData objAct = new ActualDataQuery();
objAct.Select(objAct.Year.Distinct);
    dt = objAct.LoadDataTable();
if (dt.Rows.Count > 0)
{
    FillCombo(dt);
}

I have put a breakpoint as well on the line where my datatable object is getting load, and it is showing that the distinct rows are present there multiple times (means distinct not working).
http://i.stack.imgur.com/RXYiA.jpg[^]

I have also go through the complete documentation of EntitySpaces but there's nothing regarding distinct keyword. Also I found nothing on the internet related to this problem as there's no forum for EntitySpaces2012. Any help would be hugely appreciated!
Posted

Do this:

C#
DataTable realData;
DataTable dt = new DataTable();
ActualData objAct = new ActualDataQuery();
objAct.Select(objAct.Year.Distinct);
    dt = objAct.LoadDataTable();

realData = dt.DefaultView.ToTable(true, "Year");

if (realData.Rows.Count > 0)
{
    FillCombo(realData);
}


DataView has ToTable method. First parameter defines if the rows returned need to be distinct (true) and the second is column list by which distinct should be done.

Your new datatable will have single column named Year which will contain distinct values from your view.


If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
Sohaib Ahmed 22-Dec-14 10:11am    
Thank you @Sinisa Hajnal. Although your solution is good and working, but I want to do it using EntitySpaces as there is already Distinct property present in EntitySpaces library.. I just wanted to know that how to use it (as the other answer of this question suggests)
Using Linq[^]:
C#
var qry = objAct.Select(x=>x.Year).Distinct();
    dt = qry.CopyToDataTable();
    FillCombo(dt);


For further information, please see: 101 LINQ Samples[^]

[EDIT]
EntitySpaces:
select distinct[^]
Distinct method[^]

It might look like:
C#
DataTable dt = new DataTable();
ActualData objAct = new ActualDataQuery();
objAct.Distinct = true;
objAct.Select(objAct.Year);
dt = objAct.LoadDataTable();
 
Share this answer
 
v3
Comments
Sohaib Ahmed 22-Dec-14 10:15am    
The link 'select distinct[^]' contains the real answer as it shows the way of doing it with EntitySpaces. Please edit your line of code according to EntitySpaces (as defined in the link) so that I can accept it. :)
Maciej Los 22-Dec-14 12:07pm    
Done ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900