You must Sign In to post a response.
  • Category: ASP.NET

    Linq query to find unique and duplicate rows and assign values

    From a datatable I want to find duplicate rows for a particular column and for this rows I need to increment value as 1,2,3, etc . For other rows value should be 1. How to do in linq csharp..

    Col1 col2
    And 1
    See 1
    Things 1
    She 1
    And 2
    Hi 2
    Vil 2
  • #769280
    GroupBy your datatable then Iterate each group and update your version value[like. 1,2,3] . If require add orderby using primary key

    Find the below code for your ref:

    SampleClass structure
    class SampleClass
    public int UniqueId { get; set; }

    public string DataColumn { get; set; }

    public int Version { get; set; }

    Main Method
    List<SampleClass> columnValues = new List<SampleClass>();// Load you data table here

    int cnt;
    var sList = columnValues.GroupBy(s => s.DataColumn).ToList().Select(group =>
    cnt = 0;
    return group.ToList().Select(i =>
    i.Version = ++cnt;
    return i;
    }).ToList().SelectMany(s => s).ToList<SampleClass>(); // Add orderby(uniqueId) here if required

  • #769284
    I find it complex.. please share some easy code

Sign In to post your comments