1

I have a data table dtStudent with structure as follows

Id | Name | Class | RoomNum | Subject
---------------------------------------
1  | ABC  | 5     | 10      | Maths
1  | ABC  | 5     | 10      | Science
1  | ABC  | 5     | 10      | English

As You can see, it contains different data for subject column only.

Now, I want to get the value for class and RoomNum column. As the values are same for all rows, I am using following-

var stdDetails = from r in DtTable.AsEnumerable()
select new
    {
        Class = r.Field<string>("Class").ToString(),
        RoomNum = r.Field<string>("RoomNum").ToString()
    };

What to do next? I need the values 5 and 10. Any help?

Raidri
  • 17,258
  • 9
  • 62
  • 65
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71

2 Answers2

4

Just add Distinct() call. Thus anonymous types have default Equals and GetHashCode implementations which use all their properties, you will get only distinct pairs of class and room number:

var stdDetails = (from r in DtTable.AsEnumerable()
                  select new {
                      Class = r.Field<string>("Class"),
                      RoomNum = r.Field<string>("RoomNum")
                  }).Distinct();

BTW you don't need to call ToString() on value return for field. It will be converted to type of generic parameter, i.e. to string in your case. Also it looks like you have integer values in these columns, so think about converting it to integer with r.Field<int>

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • I tried it but it is giving compile time error: `'System.Data.EnumerableRowCollection' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'System.Data.EnumerableRowCollection' could be found (are you missing a using directive or an assembly reference?)` – Microsoft DN Dec 09 '13 at 11:36
  • @MicrosoftDN make sure you have `System.Linq` namespace using in your code file – Sergey Berezovskiy Dec 09 '13 at 11:37
  • OMG, silly mistake. got it working now. But now my question is how can I get the values now. I know I can use foreach loop to loop through and get it but as it is having only one value sfter using distinct() I dont want to use foreach .. any alternative? – Microsoft DN Dec 09 '13 at 11:41
  • @MicrosoftDN you can use `First()` to get first value only. But you actually should not be able to tell how many distinct data you will have. Otherwise you simply can use `DtTable.AsEnumerable().Select(r => new { Class = .., RoomNum = ... }).First()` without distinct – Sergey Berezovskiy Dec 09 '13 at 11:42
  • But here as we know it has only one distinct value, what would be the best solution? As using 'foreach' here will be meaningless – Microsoft DN Dec 09 '13 at 11:46
  • @MicrosoftDN is data in your table static and will never change? – Sergey Berezovskiy Dec 09 '13 at 11:49
  • @MicrosoftDN if data is static then you can hardcode `Class` and `RoomNum` :) – Sergey Berezovskiy Dec 09 '13 at 12:00
  • I solved the problem using ToList() and using `stdDetails [0].Class` and `stdDetails [0].RoomNum` – Microsoft DN Dec 09 '13 at 12:01
  • 1
    @MicrosoftDN you can use `First()` as I mentioned before. `var result = ....First();` then use `result.Class` and `result.RoomNum` – Sergey Berezovskiy Dec 09 '13 at 12:05
1

You can use "Distinct()" function as explained in the link

How to use LINQ Distinct() with multiple fields

You have to select, all the fields that you need and then apply the distinct function.

Community
  • 1
  • 1
Pushkar
  • 112
  • 6