score:1
Why not just have GetKey() return the key as a string?
var a = dt.AsEnumerable().GroupBy(e => new { name = e[GetKey(e)] });
You can create the key from the values in specified columns, and make it into one string to group on:
var keyDictionary = new Dictionary<string, IEnumerable<string>>();
keyDictionary.Add("Table1", new List<string> {"Group", "Position"});
var dt = new DataTable("Table1");
dt.Columns.AddRange(new [] { new DataColumn("Id", typeof(int)), new DataColumn("Group", typeof(string)), new DataColumn("Position", typeof(string)), new DataColumn("Name", typeof(string))});
var rowItemArrays = new [] { new object[] { 1, "Alpha", "Left", "Bob" }, new object[] { 2, "Alpha", "Right", "Mary"}, new object[] { 3, "Beta", "Right", "Bill"}, new object[] { 4, "Alpha", "Right", "Larry"}};
rowItemArrays.ToList().ForEach(i => dt.Rows.Add(i));
Func<DataRow, string> GetKeys = (dataRow) => string.Join("", keyDictionary[dataRow.Table.TableName].Select(key => dataRow[key].ToString()).ToArray());
var a = dt.AsEnumerable().GroupBy(GetKeys);
You'd have to watch out for null values etc....
score:-1
var keyDictionary = new Dictionary<string, IEnumerable<string>>();
keyDictionary.Add("Table1", new List<string> {"Group", "Position"});
var dt = new DataTable("Table1");
dt.Columns.AddRange(new [] { new DataColumn("Id", typeof(int)), new DataColumn("Group", typeof(string)), new DataColumn("Position", typeof(string)), new DataColumn("Name", typeof(string))});
var rowItemArrays = new [] { new object[] { 1, "Alpha", "Left", "Bob" }, new object[] { 2, "Alpha", "Right", "Mary"}, new object[] { 3, "Beta", "Right", "Bill"}, new object[] { 4, "Alpha", "Right", "Larry"}};
rowItemArrays.ToList().ForEach(i => dt.Rows.Add(i));
Func<DataRow, string> GetKeys = (dataRow) => string.Join("", keyDictionary[dataRow.Table.TableName].Select(key => dataRow[key].ToString()).ToArray());
var a = dt.AsEnumerable().GroupBy(GetKeys);
This is best logic you can try my friend, we have lot of studies regarding this, so what answer i have written is the logic given by my professor
score:1
This is cribbed from the help files and something I haven't implemented, but should work. The problem is that you need a single class for it to compare and it uses both ToString and GetHashCode in the comparison (which is why your dictionary idea didn't work, it isn't comparing the elements of the dictionary, it's comparing the ToString and GetHashCode of it). Have GetKey return the following class and populate the keyBag of the class with your Dictionary from above:
class PortableKey
{
public Dictionary<string, object> keyBag { get; set; }
public PortableKey(Dictionary<string, object> Keys)
{
this.keyBag = Keys;
}
public override bool Equals(object obj)
{
PortableKey other = (PortableKey)obj;
foreach (KeyValuePair<string, object> key in keyBag)
{
if (other.keyBag[key.Key] != key.Value) return false;
}
return true;
}
public override int GetHashCode()
{
// hashCodes is an array of integers represented as strings. { "1", "4", etc. }
string[] hashCodes = keyBag.Select(k => k.Value.GetHashCode().ToString()).ToArray();
// hash is the Hash Codes all joined in a single string. "1,4,etc."
string hash = string.Join(",", hashCodes);
// returns a single hash code for the combined hash.
// Note, this is not guaranteed unique, nor is it intended to be so.
return hash.GetHashCode();
}
public override string ToString()
{
string[] values = keyBag.Select(k => k.Value.ToString()).ToArray();
return string.Join(",", values);
}
}
Source: stackoverflow.com
Related Query
- linq grouping by custom class
- LINQ - Entity framework code first - Grouped results to custom class
- Making a custom class availabe to LINQ queries
- LINQ Grouping by custom type not working
- LINQ Source Code Available
- How to perform group by in LINQ and get a Iqueryable or a Custom Class Object?
- LINQ to XML And Distinct Custom Class
- C# - Linq cast generic list to custom class
- Sitecore7 Linq to Sitecore only works with SearchResultItem but not with Custom Mapped Class
- Convert xml to custom class using lambda linq
- C# How to Create a Custom (dynamic) class with Dynamic Linq using XElement in runtime
- creating Linq to sqlite dbml from DbLinq source code
- ASP.NET / LINQ / EF: Async on custom distinct comparer class
- How to order list in custom Item class with linq query
- Linq on Custom Class List
- Code practice to handle empty result set in Linq to custom list
- LINQ query can't select data into a custom class object
- source code for LINQ 101 samples
- LINQ custom class query with parameters select
- LINQ Anonymous Type to ObservableCollection in Custom Class
- Populate custom List sub class from XML document via LINQ
- In Silverlight, add custom property to a generated Linq class + manage events
- LINQ to custom class when grouped by identifier
- How to access a property of a custom class using Linq to Object?
- Entity Framework LINQ Query using Custom C# Class Method - Once yes, once no - because executing on the client or in SQL?
- LINQ / XML - Storing child nodes which have different names in a custom class
- Using Linq to create a collection of a custom class from a DbSet that contains a list of grouped entities
- Calling a stored procedure using LINQ to SQL with a Custom Entity Class
- Linq query in custom class
- Use Linq to search through table for items from a custom class
More Query from same tag
- How to elegantly tackle this Attendance Form problem?
- Linq using Select and an Indexer
- Using Lambda or Linq Get data based on child table
- TreeView and Entity Framework binding
- C# LINQ - selecting a dynamic object based on properties defined at runtime
- Apply filters if Valid using lambda C#
- How to assign value to class instance members using the instance name in a select method
- Another way of solving dictionary search (without LINQ)
- Left Outer Join using DefaultIfEmpty throws NullReferenceException
- Join two lists returns IEnumerable in IEnumerable C# Entity framework
- LINQ search by text
- How to use LINQ to search and item from the database
- Passing Func to Where changes return type from IQueryable to IEnumerable
- SelectMany to Dictionary problems
- Subsonic 3.0.0.3 SQL Paging using Linq
- LINQ association - Entity Framework
- ASP.NET Add cell to Gridview row with string from code behind
- How to create Linq Expression for DateTime.Date
- Linq Split function Query
- How can anonymous types be created using LINQ with lambda syntax?
- LINQ to SQL Join issues with FirstOrDefault()
- Indexing IQueryable<int>?
- What will be the linq equivalent of db join with group by
- C# Find most common strings in string array
- Improve nested query in entity framework
- Get root descendant element using value of another element
- Making few columns NULL based on a Where condition in LINQ
- Watin IE becomes visible when using a LINQ where
- linq ordering grouped elements after groupby
- Nested Select MySQL statements to LINQ