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

Fetching Distinct Rows From Datatable using LINQ

1 Answer 747 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.
Alphaa
Top achievements
Rank 1
Alphaa asked on 14 Jun 2010, 05:38 PM
Hi All.

I have Dynamic Data table and i want to extract Distinct Rows Based on certain number of Columns

Now number of Columns is dynamic

I am trying to use LINQ to achieve this

I am using

var distinctRows = (from DataRow dRow in dt.Rows
select new {col1 = dRow["T0"], col2 = dRow["T1"], col3 = dRow["T2"], col4 = dRow["T3"] }).Distinct();

------------------(T0,T1,T2,T3) are Column Names--------
where this Datatable has 4 columns on which i need Distinct query but i need a generic solution where Number of Columns Can be flexible??

For Example if i need to Fire Distinct query for Five Columns query will become

var distinctRows = (from DataRow dRow in dt.Rows
select new {col1 = dRow["T0"], col2 = dRow["T1"], col3 = dRow["T2"], col4 = dRow["T3"] , col5 = dRow["T4"]}).Distinct();



and So on ...

how can i make this query flexible??

Any help??

Thanks

1 Answer, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 16 Jun 2010, 05:28 PM
Hi Alphaa,

 After researching your problem, we saw you are trying to invoke an object initializer with a varying number of values. Unfortunately, Microsoft LINQ support does not offer a clean way to select an anonymous type with a variable set of properties. The number of named fields for the column values cannot be variable as you explicitly give a name for each property. Even if you do not name explicitly each property still you need to specify its value as a part of the anonymous type constructor.

Don't hesitate to contact us if you have any other questions.


Kind regards,
Petko_I
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
LINQ (LINQ specific questions)
Asked by
Alphaa
Top achievements
Rank 1
Answers by
Petko_I
Telerik team
Share this question
or