Linq: pass GroupBy result to a function - c#

I need to group items in a list and then pass each group to a function for further elaboration.
This is my code:
var list = new List<MyObj>(); // list is created ad populated elsewhere in code...
var query = list.AsEnumerable();
query = query.Where(x => x.MyProp == true).Select(x => x); // query definition is way more complicated
var grp = query.GroupBy(x => new { x.Name, x.Surname }).ToList();
Here grp is of type List<IGrouping<'a, MyObj>>.
I can easily iterate through my items with:
foreach (var g in grp)
{
foreach (var o in g)
{
// here "o" is of type MyObj
}
}
but I don't know how to create a function that receives a group and iterates through its items:
foreach (var g in grp)
{
DoSomethingWithGroup(g);
}
This is because I have an anonymous type (Key) in grp definition.
I tried to replace the Key anonymous type with a custom type:
private class GrpKey
{
public string Name { get; set; }
public string Surname { get; set; }
}
/* ... */
var grp = query.GroupBy(x => new GrpKey { Name = x.Name, Surname = x.Surname }).ToList();
This way grp is of type List<IGrouping<MyGrpKey, MyObj>> instead of List<IGrouping<'a, MyObj>>.
I could then create a function:
private void DoSomethingWithGroup(IGrouping<MyGrpKey, MyObj>) { /* ... */ }
Unfortunately, this way grouping doesn't work anymore: grp now contains as many groups as items in the source list, each one with a single item.

Instead of specifying your method like you did:
private void DoSomethingWithGroup(IGrouping<MyGrpKey, MyObj>)
separate the key and the elements into their on parameters
private void DoSomethingWithGroup<T>(T groupKey, IEnumerable<MyObj> entities)
With this change you can do the following:
//Populate list with some dummy data
var list = new List<MyObj>()
{
new MyObj { Id = 1, MyProp = true, Name = "A", Surname = "B"},
new MyObj { Id = 2, MyProp = false, Name = "A", Surname = "B"},
new MyObj { Id = 3, MyProp = true, Name = "B", Surname = "B"},
new MyObj { Id = 4, MyProp = true, Name = "B", Surname = "B"},
new MyObj { Id = 5, MyProp = true, Name = "C", Surname = "B"},
};
//Perform the grouping
var groups = list
.Where(x => x.MyProp)
.GroupBy(x => new { x.Name, x.Surname })
.ToList();
//Perform some arbitrary action on the group basis
foreach (var group in groups)
{
DoSomethingWithGroup(group.Key, group);
}
If the implementation of the DoSomethignWithGroup looks like this:
void DoSomethingWithGroup<T>(T groupKey, IEnumerable<MyObj> entities)
{
Console.WriteLine(groupKey);
foreach (var entity in entities)
{
Console.WriteLine($"- {entity.Id}");
}
}
then the output will be this:
{ Name = A, Surname = B }
- 1
{ Name = B, Surname = B }
- 3
- 4
{ Name = C, Surname = B }
- 5
DotnetFiddle link

If the key is not needed in your function, you can omit that in the method. A IGrouping<TKey, TElement> is an IEnumerable<TElement> so you could just define it as:
private void DoSomethingWithGroup(IEnumerable<MyObj> items)
{
///...
}

Related

how to grab all the owners of a name

I have one plain class data like,
var data = new List<PlainData>
{
new PlainData {Name = "A", Owner = "X"},
new PlainData {Name = "A", Owner = "Y"},
new PlainData {Name = "B", Owner = "X"}
};
Here for same Name I have one or more than one owner.
Now I want to transform this data into list based owners like below class,
public class ListBasedData
{
public string Name { get; set; }
public List<string> Owners { get; set; }
}
And here I am trying to do, how to grab all the owners of a name?
List<ListBasedData> listBasedDatas = new List<ListBasedData>();
var groups = data.GroupBy(a => a.Name);
foreach (var group in groups)
{
var a = group.Key;
var b = group.ToList();
listBasedDatas.Add(new ListBasedData{Name = group.Key, Owners = });
}
List<ListBasedData> listBasedDatas = data
.GroupBy(a => a.Name)
.Select(grp => new ListBasedData
{
Name = grp.Key,
Owners = grp.Select(x => x.Owner).ToList()
})
.ToList();
The key is to use Select to perform a projection on the owners in each group, from PlainData to string.

Get count of all items in a list that have one of the properties in another list

I am wondering if/how I can do the following thing using LINQ: I have a list of objects with some properties and another list of different distinct values corresponding to a certain property.
Example:
A = [{id=1, property=1}, {id=2, property=1}, {id=3, property=2}]
B = [1, 2]
Is there a way of achieving the following thing (obtaining the counts list) using only LINQ?
var counts = new List<int>();
foreach (var b in B)
{
counts.Add(A.Where(a => a.property == b).Count();
}
Sample code:
public class MyObject
{
public MyObject(int id, int prop)
{
Id = id;
Property = prop;
}
public int Id { get; set; }
public int Property { get; set; }
public void test()
{
var A = new List<MyObject>
{
new MyObject(1, 1), new MyObject(2, 1), new MyObject(3, 2)
};
var B = new List<int>{1, 2};
// LINQ magic
// 2 objects with property 1
// 1 object with property 2
}
}
Yes, use select operators to only select the specific properties you want to compare, and then use intersect and count to get the count. Example:
var listOfObjects = new List<PocoClass>()
{
new PocoClass(){Id=1,Property=3},
new PocoClass(){Id=2,Property=2}
};
var intArray = new int[] { 1, 2, 3 };
var count = listOfObjects.Select(o => o.Property).Intersect(intArray).Count();
Sure, you can just loop through the values and, for each one, get the count of items that have Property == value.
In the sample below, I'm selecting an anonymous type that contains the Value and the Count of each item that has Property == value:
public class Data
{
public int Id { get; set; }
public int Property { get; set; }
}
public class Program
{
private static void Main()
{
var allData = new List<Data>
{
new Data {Id = 1, Property = 1},
new Data {Id = 2, Property = 1},
new Data {Id = 3, Property = 2},
};
var values = new[] {1, 2};
var results = values.Select(value =>
new {Value = value, Count = allData.Count(item => item.Property == value)});
foreach (var result in results)
{
Console.WriteLine($"{result.Count} objects with Property {result.Value}");
}
}
}
Output
You can use Count method with a predicate:
var A = new[] {new {id = 1, property = 1}, new {id = 2, property = 1}, new {id = 3, property = 2}};
var B = new[] {1, 2};
var count = B.Count(b => A.Any(a => a.property == b));
Code above will check every member in B and if at least one member in A have a property with that value it will be counted
convert B to List and run ForEach on it
B.OfType<int>().ToList().ForEach(m=>{
counts.Add(A.Where(a => a.property == m).Count();
})
This is the gist of what you need. I'm typing this without a c# compiler, so hopefully this doesn't have errors.
var results =
from a in A
join b in B on a.property equals b
group a by a.property into g
select new { Property = g.Key, Count = g.Count() }

Search in IQueryable<object>

Is it possible to search in an IQueryable
public static IQueryable<object> SearchAllFields(IQueryable<object> query, string term)
{
query = query.Where(q => q.Property1 == term);
query = query.Where(q => q.Property2 == term);
query = query.Where(q => q.Property3 == term);
return query;
}
Lets say I want to compare the search term to each of the properties that the object might have, without knowing before what properties the object might have.
Edit:
I am attempting to create a generic DataTable solution to display any tabular information that might be necessary (orders, books, customers, etc.)
To test the concept I'm using the ApplicationLogs table in my database. The DataTable looks as follows:
Lets say when typing in that search box I want to search for that value in all the columns that might be displayed. The query that populates the table:
IQueryable<object> query = (from log in db.ApplicationLog
orderby log.LogId descending
select new
{
LogId = log.LogId,
LogDate = log.LogDate.Value,
LogLevel = log.LogLevelId == 1 ? "Information" : log.LogLevelId == 2 ? "Warning" : "Error",
LogSource = log.LogSourceId == 1 ? "Www" : log.LogSourceId == 2 ? "Intranet" : "EmailNotification",
LogText = log.LogText
});
As you can see, this query will determine what the properties of the object will be. The example is taken from the logs table, but it can come from any number of tables. Then, if I want to call the generic search method from the original post:
query = DataTableHelper.SearchAllFields(query, pageRequest.Search);
You can use reflection to search through all the properties of the element, but if you want to return all rows where any property matches then you need to use predicatebuilder to build the applied query instead Where().
This example code will return both instances of Foo where A,B and C are "a". And the instances of Bar where E, F and G are "a". Also added example of anonymous type.
class Program
{
private class Foo
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
private class Bar
{
public string E { get; set; }
public string F { get; set; }
public string G { get; set; }
}
static void Main(string[] args)
{
var list = new List<Foo>
{
new Foo { A = "a", B = "a", C = "a" },
new Foo { A = "a2", B = "b2", C = "c2" },
new Foo { A = "a3", B = "b3", C = "c3" },
};
var list2 = new List<Bar>
{
new Bar { E = "a", F = "a", G = "a" },
new Bar { E = "a2", F = "b2", G = "c2" },
new Bar { E = "a3", F = "b3", G = "c3" },
};
var q1 = Filter(list.AsQueryable(), "a");
var q2 = Filter(list2.AsQueryable(), "a");
foreach (var x in q1)
{
Console.WriteLine(x);
}
foreach (var x in q2)
{
Console.WriteLine(x);
}
var queryable = list.Select(p => new
{
X = p.A,
Y = p.B,
Z = p.C
}).AsQueryable();
var q3 = Filter(queryable, "a");
foreach (var x in q3)
{
Console.WriteLine(x);
}
Console.ReadKey();
}
private static IQueryable<object> Filter(IQueryable<object> list, string value)
{
foreach (var prop in list.ElementType.GetProperties())
{
var prop1 = prop;
list = list.Where(l => Equals(prop1.GetValue(l, null), value));
}
return list;
}
}

LinQ nested lists and nested selects

Consider these two tables:
ClassID Name
1 C1
2 C2
ClassID List<CourseSession>
1 [Object that has value "A"], [Object that has value "B"]
2 [Object that has value "B"], [Object that has value "C"]
When I join these two tables in Linq, I get:
ID Name List
1 C1 [A, B]
2 C2 [A, B]
Wheras I need to expand them:
ID Name List
1 C1 A
1 C1 B
2 C2 A
2 C2 B
Linq code:
var classes = from row in t.AsEnumerable()
select new
{
ClassID = row.Field<Guid>("ClassID"),
ClassName = row.Field<string>("Name"),
};
var classCourses = from row in classes.AsEnumerable()
select new
{
ID = row.ID,
CourseSessionList = GetAllCoursesByID(row.ID).AsEnumerable()
};
//Attempt to join
var expandedClassCourse = from classRow in classes
join ccRow in classCourses
on classRow.ID equals ccRow.ID
into filteredExpandedClasses
select filteredExpandedClasses;
I'm not sure how to achieve this. Any ideas?
Something like (not sure what your model looks like):
context.CouseSessions.Where(cs => /* condition goes here */)
.Select(cs =>
new
{
Name = cs.Name,
Class = cs.Class.Name
});
or
context.Classes.Where(c => /* condition goes here */)
.SelectMany(c => c.Courses)
.Select(cs =>
new
{
Name = cs.Name,
Class = cs.Class.Name
});
I created two models based on assumption. I hope this helps.
class Info
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> List { get; set; }
}
class MyClass
{
public int Id { get; set; }
public string Name { get; set; }
public string s { get; set; }
}
static void Main(string[] args)
{
var infos = new List<Info> { new Info { Id = 1, Name = "c1", List = new List<string> { "A", "B" } }, new Info { Id = 2, Name = "c2", List = new List<string> { "A", "B" } } };
var myClasses = new List<MyClass>();
foreach (var info in infos)
{
myClasses.AddRange(info.List.Select(a => new MyClass { Id = info.Id, Name = info.Name, s = a }));
}
}
(from c in classList
join s in sessionList on c.ClassID equals s.ClassID
select new
{
ID = c.ClassID,
Name = c.Name,
SessionList = s.SessionList
})
.SelectMany(e => e.SessionList.Select(s => new
{
ID = e.ClassID,
Name = e.Name,
Session = s
}))

Remove duplicates in the list using linq

I have a class Items with properties (Id, Name, Code, Price).
The List of Items is populated with duplicated items.
For ex.:
1 Item1 IT00001 $100
2 Item2 IT00002 $200
3 Item3 IT00003 $150
1 Item1 IT00001 $100
3 Item3 IT00003 $150
How to remove the duplicates in the list using linq?
var distinctItems = items.GroupBy(x => x.Id).Select(y => y.First());
var distinctItems = items.Distinct();
To match on only some of the properties, create a custom equality comparer, e.g.:
class DistinctItemComparer : IEqualityComparer<Item> {
public bool Equals(Item x, Item y) {
return x.Id == y.Id &&
x.Name == y.Name &&
x.Code == y.Code &&
x.Price == y.Price;
}
public int GetHashCode(Item obj) {
return obj.Id.GetHashCode() ^
obj.Name.GetHashCode() ^
obj.Code.GetHashCode() ^
obj.Price.GetHashCode();
}
}
Then use it like this:
var distinctItems = items.Distinct(new DistinctItemComparer());
If there is something that is throwing off your Distinct query, you might want to look at MoreLinq and use the DistinctBy operator and select distinct objects by id.
var distinct = items.DistinctBy( i => i.Id );
This is how I was able to group by with Linq. Hope it helps.
var query = collection.GroupBy(x => x.title).Select(y => y.FirstOrDefault());
An universal extension method:
public static class EnumerableExtensions
{
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> keySelector)
{
return enumerable.GroupBy(keySelector).Select(grp => grp.First());
}
}
Example of usage:
var lstDst = lst.DistinctBy(item => item.Key);
You have three option here for removing duplicate item in your List:
Use a a custom equality comparer and then use Distinct(new DistinctItemComparer()) as #Christian Hayter mentioned.
Use GroupBy, but please note in GroupBy you should Group by all of the columns because if you just group by Id it doesn't remove duplicate items always. For example consider the following example:
List<Item> a = new List<Item>
{
new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
new Item {Id = 2, Name = "Item2", Code = "IT00002", Price = 200},
new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
new Item {Id = 3, Name = "Item3", Code = "IT00004", Price = 250}
};
var distinctItems = a.GroupBy(x => x.Id).Select(y => y.First());
The result for this grouping will be:
{Id = 1, Name = "Item1", Code = "IT00001", Price = 100}
{Id = 2, Name = "Item2", Code = "IT00002", Price = 200}
{Id = 3, Name = "Item3", Code = "IT00003", Price = 150}
Which is incorrect because it considers {Id = 3, Name = "Item3", Code = "IT00004", Price = 250} as duplicate. So the correct query would be:
var distinctItems = a.GroupBy(c => new { c.Id , c.Name , c.Code , c.Price})
.Select(c => c.First()).ToList();
3.Override Equal and GetHashCode in item class:
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public int Price { get; set; }
public override bool Equals(object obj)
{
if (!(obj is Item))
return false;
Item p = (Item)obj;
return (p.Id == Id && p.Name == Name && p.Code == Code && p.Price == Price);
}
public override int GetHashCode()
{
return String.Format("{0}|{1}|{2}|{3}", Id, Name, Code, Price).GetHashCode();
}
}
Then you can use it like this:
var distinctItems = a.Distinct();
Use Distinct() but keep in mind that it uses the default equality comparer to compare values, so if you want anything beyond that you need to implement your own comparer.
Please see http://msdn.microsoft.com/en-us/library/bb348436.aspx for an example.
Try this extension method out. Hopefully this could help.
public static class DistinctHelper
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var identifiedKeys = new HashSet<TKey>();
return source.Where(element => identifiedKeys.Add(keySelector(element)));
}
}
Usage:
var outputList = sourceList.DistinctBy(x => x.TargetProperty);
List<Employee> employees = new List<Employee>()
{
new Employee{Id =1,Name="AAAAA"}
, new Employee{Id =2,Name="BBBBB"}
, new Employee{Id =3,Name="AAAAA"}
, new Employee{Id =4,Name="CCCCC"}
, new Employee{Id =5,Name="AAAAA"}
};
List<Employee> duplicateEmployees = employees.Except(employees.GroupBy(i => i.Name)
.Select(ss => ss.FirstOrDefault()))
.ToList();
Another workaround, not beautiful buy workable.
I have an XML file with an element called "MEMDES" with two attribute as "GRADE" and "SPD" to record the RAM module information.
There are lot of dupelicate items in SPD.
So here is the code I use to remove the dupelicated items:
IEnumerable<XElement> MList =
from RAMList in PREF.Descendants("MEMDES")
where (string)RAMList.Attribute("GRADE") == "DDR4"
select RAMList;
List<string> sellist = new List<string>();
foreach (var MEMList in MList)
{
sellist.Add((string)MEMList.Attribute("SPD").Value);
}
foreach (string slist in sellist.Distinct())
{
comboBox1.Items.Add(slist);
}
When you don't want to write IEqualityComparer you can try something like following.
class Program
{
private static void Main(string[] args)
{
var items = new List<Item>();
items.Add(new Item {Id = 1, Name = "Item1"});
items.Add(new Item {Id = 2, Name = "Item2"});
items.Add(new Item {Id = 3, Name = "Item3"});
//Duplicate item
items.Add(new Item {Id = 4, Name = "Item4"});
//Duplicate item
items.Add(new Item {Id = 2, Name = "Item2"});
items.Add(new Item {Id = 3, Name = "Item3"});
var res = items.Select(i => new {i.Id, i.Name})
.Distinct().Select(x => new Item {Id = x.Id, Name = x.Name}).ToList();
// now res contains distinct records
}
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}

Categories

Resources