Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have a query I need to run for each item in the last and was wanting to be able to pass a variable into my group by clause.

public string[][] GetAttributes(int attributelenght, string[] attributes)
        {
            string[][] result = new string[attributelenght][];

            string parm = "";
          
            foreach (var a in attributes)
            {
                for (int i = 0; i <= attributelenght;)
                {
                   
                       var varible = "c." + a; //c.Colour 

                        var feild = (from c in _auth.ClothingItems
                                     group c by varible into g // change each loop to match attibutes
                                     select g).ToList();

                        foreach (var f in feild)
                        {
                            parm = parm + "," + f.Key; // black , blue, yellow, pink, red

                        }
                }


            }


            return result;
        }


however, this is not working and it is recognising the variable as a field name. Could anyone help me please

What I have tried:

public string[][] GetAttributes(int attributelenght, string[] attributes)
        {
            string[][] result = new string[attributelenght][];

            string parm = "";
          
            foreach (var a in attributes)
            {
                for (int i = 0; i <= attributelenght;)
                {
                   
                       var varible = "c." + a; //c.Colour 

                        var feild = (from c in _auth.ClothingItems
                                     let var = varible
                                     group c by var into g // change each loop to match attibutes
                                     select g).ToList();

                        foreach (var f in feild)
                        {
                            parm = parm + "," + f.Key; // black , blue, yellow, pink, red

                        }
                }


            }


            return result;
        }
Posted
Comments
F-ES Sitecore 7-Nov-18 11:13am    
I googled "Dynamic group by linq" and found a few solutions. Have you done that yourself and tried any of them?
Member 12285600 7-Nov-18 11:27am    
I tried a couple like -

https://stackoverflow.com/questions/17678197/linq-grouping-dynamically
https://stackoverflow.com/questions/33286297/linq-groupby-with-a-dynamic-group-of-columns

but i didnt really understand how they go to the answer so im struggling to apply it to my example

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