?
Solved

LINQ - Extension methods and Func<T,TResult>

Posted on 2012-08-17
6
Medium Priority
?
565 Views
Last Modified: 2012-09-16
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();
        }
0
Comment
Question by:Dhaest
  • 4
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38303884
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?
0
 
LVL 53

Author Comment

by:Dhaest
ID: 38304105
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();
        }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38304481
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 53

Author Comment

by:Dhaest
ID: 38304503
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.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 0 total points
ID: 38310935
Perhaps not the best solution, but for the moment I solved it by using a private variable which will be filled with the string.

        private static string[] groupByPropertyList;

        public static String GroupData(ManagementInfoListData.ManagementInfoListRow dataRow)
        {
           
            stringBuilder.Remove(0, stringBuilder.Length);
            // we always group on month of the transferDate
            stringBuilder.Append(((DateTime)dataRow[ApplicationConstants.TransferDate]).Month);


            foreach (String column in groupByPropertyList)
            {
                stringBuilder.Append(",");
                stringBuilder.Append(dataRow[column].ToString());
            }

            return stringBuilder.ToString();
        }
0
 
LVL 53

Author Closing Comment

by:Dhaest
ID: 38402958
Implemented this solution which works very well
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question