Today I learned something new so I'm just sharing a bit for anyone else that might be interested.
I needed to process rows in a DataSet where one column contain some binary data that needed to be encoded and then filtered on. I had existing code (non Linq) that did the job but since I was relooking at the code (aka refactoring) I thought I might as well change it to use Linq.
The original code looked something like this:
List<string> IDs = new List<string>();
foreach (DataRow row in spool.Tables[0].Rows)
{
string context = Bytes2String( (byte[])row["theData"]);
if (context.Contains("someText"))
{
IDs.Add(row["ID"].ToString());
}
}
----------------------------------------------------------
Then I changed it to look like this
IDs.AddRange(
from s in spool.Tables[0].Rows
where Bytes2String((byte[])s["theData"]).Contains("someText")
select s["ID"].ToString()
);
------------------------------------------------------------
But this gave me a compiler error that stated:
Error 3 Could not find an implementation of the query pattern for source type 'System.Data.DataRowCollection'. 'Where' not found. Consider explicitly specifying the type of the range variable 's'.
After some searching for a fix I found the solution - to explicitly speciify the type of the return types (as stated in the error message):
IDs.AddRange(
from DataRow s in spool.Tables[0].Rows
where Bytes2String((byte[])s["theData"]).Contains("someText")
select s["ID"].ToString()
);
There is probably some more refactoring and optimization I can do to this but at least it works! Hey you learn something new each day (even when you're getting older and learn slower..)