Jui Test
Jui Test

Reputation: 2409

Fetch distinct record from dataset/table using linq to dataset/datatable

I am fetching record from database and store a result in dataset.

My dataset like this

Sid Table userid par1 par2 par3
274 tbl1  43     0    0    0
232 tbl1  43     1    2    0
232 tbl1  43     1    2    1
232 tbl2  43     1    2    0
232 tbl2  43     1    2    1

I want to show all 6 column but distinct record.Distinct should be on Sid, Table and userid.I want output like this

Sid Table userid par1 par2 par3
 274 tbl1  43     0    0    0
 232 tbl1  43     1    2    0
 232 tbl2  43     1    2    0

Does it possible through linq to dataset/datatable. I am unable to get AsEnumerable method on dataset but getting on datatable.

Upvotes: 1

Views: 68

Answers (1)

drunkcoder
drunkcoder

Reputation: 321

I'm confused with the question but is this want you want?

yourDatatable.Rows.Cast<DataRow>()
.GroupBy(r => new { Sid = r.Field<int>("Sid"), userid = r.Field<int>("userid"), Table = r.Field<string>("Table") })
.Select(e => e.FirstOrDefault())
.Select(grp => new
{
    Sid = grp.Field<int>("Sid"),
    userid = grp.Field<int>("userid"),
    Table = grp.Field<string>("Table"),
    par1 = grp.Field<int>("par1"),
    par2 = grp.Field<int>("par2"),
    par3 = grp.Field<int>("par3")
});

Upvotes: 1

Related Questions