Using LINQ Join Extension-Method on List & DataRowCollection
I'd like to Join
a List
and a DataRowCollection
,
my Code so far looks like this:
attrList.Join<Attribute, DataRow, string, Attribute>(
dt.Rows,
attr => attr.Name,
dataRow => dataRow[0].ToString(),
(a, b) => new Attribute(a.Name, a.Value, b[1].ToString()));
attrList is a List of type MyProject.Attribute, which contains a string-Property "Name", the DataRowCollection comes from a DataTable (duh) and contains 2 Values, Index 0 contains a string that should match the Name-Property of Attribute (which is why I'm using join) and Index 1 contains a 2nd string-value which will be appended to the existing Attribute using an overloaded ctor.
Unfortunately, this does not work.
Error:
[...]List<[...]> does not contain a definition for Join and the best extension method overload [...] has some invalid arguments.
I simply can't find wha开发者_运维百科t's supposed to be wrong here.
Here's the Constructor of Attribute:
public Attribute(string name, string value, string control)
{
this.name = name;
this.value = value;
this.control = control;
}
DataTable.Rows is not generic i.e. it does not implement IEnumerable<T>
so you can't use it in Linq, try below:
attrList.Join<Attribute, DataRow, string, Attribute>(
dt.AsEnumerable(),
attr => attr.Name,
dataRow => dataRow[0].ToString(),
(a, b) => new Attribute(a.Name, a.Value, b[1].ToString()));
精彩评论