开发者

I need to filter distinct value from datatable using IEnumerable LINQ

I am new to LINQ concept. i need to know how to take the distinct value in date table using LINQ

Sample Code :

IEnumerable<DataRow> query1 =
                          (from pmh in pshTable.AsEnumerable()
                           where
                           (pmh.Field<String>("DeletedType") != "D" && pmh.Field<String>("code").ToUpper() != "XXX.XX")
                           select pmh).OrderBy(a => a.Field<String>("Description")).Distinct();

in this coding i can able to get distinct row .but i need only particular column "description" v开发者_StackOverflow中文版alue is distinct . please let me know .. value


What do you want for example you have bellow table:

 ID  | Description
 ----------------
 1   | Test
 2   | Test
 3   | Sample
 4   | Sample

What do you want to get by your specific Distinct? for example {{1,Test},{3,Sample}} or {{1,Test},{4,Sample}} or ...

but you can use GroupBy and Select to do Similar to what you want:

        (from pmh in pshTable.AsEnumerable()
                                   where
                                   (pmh.Field<String>("DeletedType") != "D" 
                                   && pmh.Field<String>("code").ToUpper() != "XXX.XX")
                                   select pmh).OrderBy(a => a.Field<String>("Description"))
        .GroupBy(p=>p.Field<string>("Description"))
        .Select(p=>p.FirstOrDefault());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜