Click here to Skip to main content
15,608,989 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query that I want to run where I GroupBy based on TeamID and ActivityName. The TeamID is an int and the ActivityName is a string. I want to perform a GroupBy query where the key in the GroupBy is (TeamID, ActivityName) with the case being ignored in the ActivityName. The code below returns 4 groups as the the activities are all different based on content or case, I want there to be 2 by ignoring the case. I have tried adding StringComparer.InvariantCultureIgnoreCase to the query but I get a compilation error stating "The type arguments for method 'System.Linq.Enumerable.GroupBy<tsource,tkey,tresult>(System.Collections.Generic.IEnumerable<tsource>, System.Func<tsource,tkey>, System.Func<tkey,system.collections.generic.ienumerable><tsource>,TResult>' cannot be inferred from the usage. Try specifying the type arguments explicitly." I have also tried using ToUpper() in the query but with no success.
I can't figure out how this should be done, any help would be appreciated.

What I have tried:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            PerformQuery();
        }

        public void PerformQuery()
        {
            List<team> teams = new List<team>();
            teams.Add(new Team { TeamId = 1, ActivityName = "Football" });
            teams.Add(new Team { TeamId = 1, ActivityName = "football" });
            teams.Add(new Team { TeamId = 2, ActivityName = "Rugby" });
            teams.Add(new Team { TeamId = 2, ActivityName = "rugby" });

            var activities = teams
                .GroupBy(e => new { e.TeamId, e.ActivityName });
        }
    }

    public class Team 
    {
        public int TeamId { get; set; }
        public string ActivityName { get; set; }
    } 

Posted
Updated 24-Apr-16 23:16pm
v2

Try:
C#
var activities = teams.GroupBy(e => new { e.TeamId, e.ActivityName.ToLower() });


:doh: - I need more coffee...
Try this:
C#
public class Team
    {
    public int TeamId { get; set; }
    public string ActivityName { get; set; }
    public string ActivityNameLower { get { return ActivityName.ToLower(); } }
    }

And then:
C#
var activities = teams.GroupBy(e => new { e.TeamId, e.ActivityNameLower });
 
Share this answer
 
v2
Comments
alywaly 25-Apr-16 5:30am    
Perfect, Thank you!
OriginalGriff 25-Apr-16 5:35am    
You're welcome!
khaleelsyed 21-Jul-20 5:42am    
will not throw exeception if value is null in the string
OriginalGriff 21-Jul-20 5:58am    
Of course it won't: it won't look at the content unless you tell it to ...
check this

C#
var activities = teams.GroupBy(e => new { TeamId = e.TeamId, ActivityName = e.ActivityName.ToLower() }).ToList();
foreach (var item in activities)
{
    var key = item.Key;
    var items = item.ToList();

}
 
Share this answer
 
Comments
alywaly 25-Apr-16 5:30am    
Perfrect, Thank you!
Karthik_Mahalingam 25-Apr-16 5:34am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900