0
votes

What is wrong with this query?

var model =
    SessionObjectsMSurvey.ContractList
    .Where(y => y.ContractTitle.ToUpper().Contains(upper))
    .GroupBy(g => new 
    {
        g.BriefTitle,
        g.ContractId
    })
    .Select(
        x => new
        {
            label = x.BriefTitle,
            id = x.ContractId.ToString()
        }).Take(20);

SessionObjectsMSurvey.ContractList is an IEnumerable collection.

This won't compile, I get;

Error 13 'System.Linq.IGrouping' does not contain a definition for 'BriefTitle' and no extension method 'BriefTitle' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)

2

2 Answers

4
votes

I think you need to insert .Key at two places in your .Select(...) call. The result of the .GroupBy(...) is a queryable of IGrouping and that interface contains a .Key property to let you access the key of your group.

var model =
    SessionObjectsMSurvey.ContractList
    .Where(y => y.ContractTitle.ToUpper().Contains(upper))
    .GroupBy(g => new 
    {
        g.BriefTitle,
        g.ContractId
    })
    .Select(
        x => new
        {
            label = x.Key.BriefTitle,                 // Here
            id = x.Key.ContractId.ToString()          // And here
        }).Take(20);
1
votes

Use Key like this:

        label = x.Key.BriefTitle,                 
        id = x.Key.ContractId.ToString()         

See this link.