Skip to content Skip to sidebar Skip to footer

Linq To Sql / Filter Duplicates

i have a view in my sql server 2012 with a couple of duplicates and i want to sort them by the newest and filter all others - can anyone help me? My viewin my SQL Server 2012: GUID

Solution 1:

You can do it by grouping your elements by 2 columns. (number and name). Then access the grouped data. You can do it somehow like that:

var query =
        from col in viewData
        group col bynew
    {
        col.name,
        col.number,

    } into groupedCol
    selectnewviewData()
    {
        number = groupedCol.Key.number,
        name = groupedCol.Key.name,
        datetime = groupedCol.OrderBy( dateCol => dateCol.datetime).First()

    };

Solution 2:

var res = list.GroupBy(c => c.name).Select(group => group.OrderBy( c1 => c1.datetime).First()).ToList();

This should work as long as datetime is stored as an instance of DateTime, instead of string.

Post a Comment for "Linq To Sql / Filter Duplicates"