linq

How to sort a collection based on type in LINQ

試著忘記壹切 提交于 2020-08-27 05:22:06
问题 I have a collection like this, Class Base{} Class A : Base {} Class B : Base {} List<Base> collection = new List<Base>(); collection.Add(new A()); collection.Add(new B()); collection.Add(new A()); collection.Add(new A()); collection.Add(new B()); Now I want to sort the collection based on type (A/B). How I can do this? Please help me. 回答1: You can use the type information itself: collection.Sort( (a,b) => { bool aType = a.GetType() == typeof(A); bool bType = b.GetType() == typeof(A); return

Using LINQ GroupBy to group by reference objects instead of value objects

匆匆过客 提交于 2020-08-26 09:56:13
问题 I want to GroupBy multiple objects in a list of records and not simply multiple values. I am having trouble getting grouping to work with reference type objects. I have a collection of objects that contain Room, Type, and DateTime. Room, Type, and DateTime all have properties associated with them. I've already added the IEquateable interface to the room and the type thinking that would be enough to with with group by. var groups = collection .Where(g => g.Stage == InventoryStage.StageA)

Linq .GroupBy() with count

一世执手 提交于 2020-08-25 09:19:47
问题 I have a table that I need to summarize in a report. This is my sample table. Orders _____________________________________ CustomerId | CustomerName | OrderType ___________|______________|__________ 1 | Adam | Shoe 1 | Adam | Shoe 1 | Adam | Shoe 1 | Adam | Hat 1 | Adam | Hat 2 | Bill | Shoe 2 | Bill | Hat 3 | Carl | Sock 3 | Carl | Hat I am trying to summarize this to pass back in my viewmodel without a loop. This is the result that I am attempting to achieve. CustomerName | Shoe | Hat |

How to Except Property instead of Select in Lambda LINQ

自闭症网瘾萝莉.ら 提交于 2020-08-25 05:09:29
问题 I have a code that fetching a table. Here's my sample code: public IQueryable<MyItem> MyItems(){ return context.MyItem; } Here are the sample properties of MyItem public int Id {get;set;} public byte[] Image {get;set;} public string Name {get;set;} Since, byte[] can have multiple characters, I don't want to include then in searching because it will take so long if I have a records like 10,000 items. Typically, I would Select like this: public IQueryable<MyItem> MyItems(){ return context

LINQ: How to join nested Lists in an ObjectCollection

半世苍凉 提交于 2020-08-24 10:48:25
问题 is it possible to define a LINQ statement for the following problems WITHOUT using a foreach loop? public class GroupObject { public String name; public List<String> letters; public void test() { List<GroupObject> myGroups = new List<GroupObject> { new GroupObject { name="test1", letters=new List<String>{"x","y","z"} }, new GroupObject { name="test2", letters=new List<String>{"l","m","n"} }, new GroupObject { name="test3", letters=new List<String>{"m","x","z"} } }; // LINQ problem 1: how to

Get grouped comma separated values with linq

こ雲淡風輕ζ 提交于 2020-08-24 06:33:27
问题 I would like a third column "items" with the values that are grouped. var dic = new Dictionary<string, int>(); dic.Add("a", 1); dic.Add("b", 1); dic.Add("c", 2); dic.Add("d", 3); var dCounts = (from i in dic group i by i.Value into g select new { g.Key, count = g.Count()}); var a = dCounts.Where(c => c.count>1 ); dCounts.Dump(); a.Dump(); This code results in: Key Count 1 2 2 1 3 1 I would like these results: Key Count Items 1 2 a, b 2 1 c 3 1 d 回答1: var dCounts = (from i in dic group i by i

Get grouped comma separated values with linq

☆樱花仙子☆ 提交于 2020-08-24 06:33:06
问题 I would like a third column "items" with the values that are grouped. var dic = new Dictionary<string, int>(); dic.Add("a", 1); dic.Add("b", 1); dic.Add("c", 2); dic.Add("d", 3); var dCounts = (from i in dic group i by i.Value into g select new { g.Key, count = g.Count()}); var a = dCounts.Where(c => c.count>1 ); dCounts.Dump(); a.Dump(); This code results in: Key Count 1 2 2 1 3 1 I would like these results: Key Count Items 1 2 a, b 2 1 c 3 1 d 回答1: var dCounts = (from i in dic group i by i

Difference between PredicateBuilder<True> and PredicateBuilder<False>?

半城伤御伤魂 提交于 2020-08-24 05:32:10
问题 I have the code: var predicate = PredicateBuilder.True<Value>(); predicate = predicate.And(x => x.value1 == "1"); predicate = predicate.And(x => x.value2 == "2"); var vals = Value.AsExpandable().Where(predicate).ToList(); If I have PredicateBuilder.True<Value>() , it brings back what I expect, but if I have PredicateBuilder.False<Value>() , it brings back 0 records. Can someone explain what the the difference is and why in one scenario I get back 0 records an in the other I get what I expect.

Difference between PredicateBuilder<True> and PredicateBuilder<False>?

我只是一个虾纸丫 提交于 2020-08-24 05:30:06
问题 I have the code: var predicate = PredicateBuilder.True<Value>(); predicate = predicate.And(x => x.value1 == "1"); predicate = predicate.And(x => x.value2 == "2"); var vals = Value.AsExpandable().Where(predicate).ToList(); If I have PredicateBuilder.True<Value>() , it brings back what I expect, but if I have PredicateBuilder.False<Value>() , it brings back 0 records. Can someone explain what the the difference is and why in one scenario I get back 0 records an in the other I get what I expect.

Custom OrderBy on a List<T>

ぃ、小莉子 提交于 2020-08-22 08:09:30
问题 I'm trying to figure out the best way to custom sort a List. Lets say that T is a Object with a date(DateTime?) property and a status(string) property. I have 3 cases... "Urgent": I want these at the top of the list, no particular order date = null status = "Urgent" "Normal": I want these ordered by date after the Urgent cases date = any valid date/time status = "On Time" "Later": I want these at the bottom of the list, no particular order date = null status = "Later" Any thoughts? Should I