Link to home
Start Free TrialLog in
Avatar of Dirk Haest
Dirk HaestFlag for Belgium

asked on

LINQ - Extension methods and Func<T,TResult>

I have the following code which works fine

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);

Open in new window

Now I would like to change it so that I can pass the columnNames instead that it's in the groupData-Function



Something like

        public static String GroupData(ManagementInfoListData.ManagementInfoListRow dataRow, String[] columnNames )
        {
            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();
        }
Avatar of kaufmed
kaufmed
Flag of United States of America image

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?
Avatar of Dirk Haest

ASKER

The adaptation to the method is no issue. See below.
But how do I need to call it ?
            Func<ManagementInfoListData.ManagementInfoListRow, String[], String> groupingFunctionTwo = GroupDataTwo;

            var groupedDataRowTwo = managementInfoListDataTable.AsQueryable().GroupBy(groupingFunctionTwo);

If so, I get an error: the type arguments cannot be infered ...


public static String GroupDataTwo(ManagementInfoListData.ManagementInfoListRow dataRow, String[] columnNames)
        {
            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();
        }
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:

Func<ManagementInfoListRow, String> fun;

Open in new window


I'll have to think about how to achieve your desired goal.
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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Implemented this solution which works very well