Dirk Haest
asked on
LINQ - Extension methods and Func<T,TResult>
I have the following code which works fine
Something like
public static String GroupData(ManagementInfoLi stData.Man agementInf oListRow dataRow, String[] columnNames )
{
stringBuilder.Remove(0, stringBuilder.Length);
// we always group on month of the transferDate
stringBuilder.Append(((Dat eTime)data Row["Trans ferDate"]) .Month);
foreach (String column in columnNames)
{
stringBuilder.Append(",");
stringBuilder.Append(dataR ow[column] .ToString( ));
}
return stringBuilder.ToString();
}
Func<ManagementInfoListData.ManagementInfoListRow, String> groupingFunction = GroupData;
public static String GroupData(ManagementInfoListData.ManagementInfoListRow dataRow)
{
String[] columnNames = new[] {"QuestionTypeCv"};
stringBuilder.Remove(0, stringBuilder.Length);
// we always group on month of the transferDate
stringBuilder.Append(((DateTime)dataRow["TransferDate"]).Month);
foreach (String column in columnNames)
{
stringBuilder.Append(",");
stringBuilder.Append(dataRow[column].ToString());
}
return stringBuilder.ToString();
}
var groupedDataRow = managementInfoListDataTable.AsQueryable().GroupBy(groupingFunction);
Now I would like to change it so that I can pass the columnNames instead that it's in the groupData-FunctionSomething like
public static String GroupData(ManagementInfoLi
{
stringBuilder.Remove(0, stringBuilder.Length);
// we always group on month of the transferDate
stringBuilder.Append(((Dat
foreach (String column in columnNames)
{
stringBuilder.Append(",");
stringBuilder.Append(dataR
}
return stringBuilder.ToString();
}
OK, so what am I missing? It seems as though you simply want to add a parameter to your method (which it looks like you've already worked out). Does that code not work?
ASKER
The adaptation to the method is no issue. See below.
But how do I need to call it ?
Func<ManagementInfoListDat a.Manageme ntInfoList Row, String[], String> groupingFunctionTwo = GroupDataTwo;
var groupedDataRowTwo = managementInfoListDataTabl e.AsQuerya ble().Grou pBy(groupi ngFunction Two);
If so, I get an error: the type arguments cannot be infered ...
public static String GroupDataTwo(ManagementInf oListData. Management InfoListRo w dataRow, String[] columnNames)
{
stringBuilder.Remove(0, stringBuilder.Length);
// we always group on month of the transferDate
stringBuilder.Append(((Dat eTime)data Row["Trans ferDate"]) .Month);
foreach (String column in columnNames)
{
stringBuilder.Append(",");
stringBuilder.Append(dataR ow[column] .ToString( ));
}
return stringBuilder.ToString();
}
But how do I need to call it ?
Func<ManagementInfoListDat
var groupedDataRowTwo = managementInfoListDataTabl
If so, I get an error: the type arguments cannot be infered ...
public static String GroupDataTwo(ManagementInf
{
stringBuilder.Remove(0, stringBuilder.Length);
// we always group on month of the transferDate
stringBuilder.Append(((Dat
foreach (String column in columnNames)
{
stringBuilder.Append(",");
stringBuilder.Append(dataR
}
return stringBuilder.ToString();
}
This is because the GroupBy method expects a function that takes in a ManagementInfoListRow and returns a String (the key). In other words, the function you pass to GroupBy should be:
I'll have to think about how to achieve your desired goal.
Func<ManagementInfoListRow, String> fun;
I'll have to think about how to achieve your desired goal.
ASKER
I agree with your conclusion above, but what I want is to use the groupBy method (which works as I want), but instead of hardcoding the fieldnames in the GroupData-function, I want to pass it to the function.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Implemented this solution which works very well