Reputation: 1574
I recently switched from VB to C# and have, for the most part, been able to make the transition without too much trouble. Currently I am working on converting a VB.Net program into C# and am having trouble with a couple of code blocks that include Linq.
This is one of the VB.Net lines of code that I am having trouble with:
Dim emails = From em In dt Group By email = em(1) Into Emailers = Group
I have tried to do the same thing in C# like this:
var emails =
from em in dt
group emails = em[1]
into Emailers = group;
I am getting a compile error on the dt, which says Could not find an implementation of the query pattern for source type 'System.Data.DataTable'.'GroupBy' not found. VS also has issues with the whole into Emailers = group;
I have tried to get this right using examples from my search but have not been able to get it right. How should that be converted?
Upvotes: 1
Views: 202
Reputation: 1499760
There are two problems:
DataTable
directly; DataTable
doesn't implement IEnumerable<T>
which is why it's failing in C#. (Presumably VB handles DataTable
specially somehow.)My VB is rubbish, particularly for LINQ, so I don't understand the query - but it looks like you're just trying to group the rows by one column. If that's the case, it's as simple as:
var emails = from em in dt.AsEnumerable()
group em by em[1];
Or even:
var emails = dt.AsEnumerable().GroupBy(em => em[1]);
Make sure you have:
using System.Data;
in your code, so that it'll pick up AsEnumerable
from DataTableExtensions
.
Upvotes: 7
Reputation: 4907
Check this:
var emails =
from em in dt.AsEnumerable()
group emails = em[1]
into Emailers = group;
Upvotes: 0