none
Group by Dynamic LINQ RRS feed

  • Question

  • Hello

    Ive spent about two days trying to do this but i can't work it out or understand previously suggested solutions to similar problems. 

    I have a global declared array which contains the field names of my table 

     string[] attributes = new string[] { "Item", "Occasion", "Style", "Fit", "KeyWord", "Brand", "Colour", "SleeveLength", "Length", "Material"};



    For each of these items i was wanting to return a grouped by list of the contents in the database and store that in an array 

    string[][] attributeValues = GetAttributes(attributelenght, attributes);
     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 = "l."+a; //eg c.Colour or c.Brand

                        //var feild1 = from c in _auth.ClothingItems
                        //            group c by attributes[i];

                        //var test = _auth.ClothingItems.Select(l => a).ToList();

                        //var f1 = (from c in _auth.ClothingItems
                        //          from n in attributes
                        //          group c by n into g
                        //          select new
                        //          {
                        //              Feild = g.Key,
                        //              List = g.ToList()
                        //          }).ToList();

                        //var f2 = (from f in f1
                        //         from n in attributes
                        //         group f.List by n into g   
                        //         select g).ToList();

                        //List<ClothingItem> list = new List<ClothingItem>();
                        //IEnumerable<IGrouping<int, ClothingItem>> groups = list.GroupBy(x => x.ImageID);
                        //IEnumerable<ClothingItem> smths = groups.SelectMany(group => group);
                        //List<ClothingItem> newList = smths.ToList();


                        var arg = Expression.Parameter(typeof(ClothingItem), "item");
                        var body = Expression.Property(arg, a);
                        var lambda = Expression.Lambda<Func<ClothingItem, string>>(body, arg);
                        var keySelector = lambda.Compile();

                        var result = _auth.ClothingItems.GroupBy(keySelector);
                        //var feild = _auth.ClothingItems.GroupBy(e => e.GetType().GetProperty(varible).ToString()).ToDictionary(e => e.Key, e => e.ToList());

                        foreach (var f in newList)
                        {
                            parm = parm + "," + f;

                        }

                        result[i] = new string[] { parm };
                        i++;

                    }
                }

    But nothing im doing is working, I would really appreciate any help.

    Thanks



    • Edited by user1666 Wednesday, November 14, 2018 3:03 PM
    Wednesday, November 14, 2018 3:01 PM

All replies

  • Hi user1666,

    Thank you for posting here.

    For your question, I try to test the code. But there are some variables defined by yourself. Please provide the code sample which could reproduce the error for us to test. And if it is possible, please provide the data and what you want to get.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 15, 2018 8:29 AM
    Moderator