Click here to Skip to main content
15,891,424 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Case 1 works fine, but how can I make case 2 work?
It won't build on Key.A or Key.B
C#
IEnumerable<IGrouping<object, DataRow>> result = null;
            Func<DataRow, bool> predicate = null;
            switch (fields.Length)
            {
                case 2:
                    result = dt.AsEnumerable().GroupBy(x => new { A = x[fields[0]], B = x[fields[1]] });
                    predicate = x => x[fields[0]].Equals(result.FirstOrDefault().Key.A) && x[fields[1]].Equals(result.FirstOrDefault().Key.B);
                    return result;
                default://case 1
                    result = dt.AsEnumerable().GroupBy(x => x[fields[0]]);
                    predicate = x => x[fields[0]].Equals(result.FirstOrDefault().Key);
                    return result;
            }
Posted
Updated 25-Jun-14 5:11am
v2
Comments
Sergey Alexandrovich Kryukov 25-Jun-14 13:18pm    
No, "default" is not case 1. It is case 0, case 3, etc. Default is just like "else", so better use if-else.
And it would be good if you explain what do you want to achieve. Predicate — for what purpose? and so on...
—SA
sirol81 26-Jun-14 4:27am    
if I have two primary keys, i want to group by field A and field B (the p-key). Then I want to identify all rows having those two keys with the predicate in a linq where, but i can't build the predicate with fields A and B. I don't know beforehand the fields name, that's why i would like to dinamically build both keyselector and predicate

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