Fetching Distinct Rows From Datatable using LINQ

2 posts, 0 answers
  1. Alphaa
    Alphaa avatar
    1 posts
    Member since:
    Jun 2010

    Posted 14 Jun 2010 Link to this post

    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
  2. Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 16 Jun 2010 Link to this post

    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.
  3. DevCraft banner
Back to Top