linq

How to pass lambda expressions as parameter in C#

最后都变了- 提交于 2021-02-04 19:09:15
问题 I am beginner at using lambda expressions. I have a list of dealers, foreach dealer I have to calculate grade. The request is that the grade calculation to be separated into a separate method. So I am writing the below two methods, however I am unable to pass parameters to CalculateGrade() method, public IEnumerable<Dealers> GetDealerGrades(IEnumerable<Dealers> gradeData) { return gradeData .GroupBy(row => new { row.Name }) .Select(g => new Dealers { Name = g.Key.Name, TotalPoints =

How to pass lambda expressions as parameter in C#

北慕城南 提交于 2021-02-04 19:09:06
问题 I am beginner at using lambda expressions. I have a list of dealers, foreach dealer I have to calculate grade. The request is that the grade calculation to be separated into a separate method. So I am writing the below two methods, however I am unable to pass parameters to CalculateGrade() method, public IEnumerable<Dealers> GetDealerGrades(IEnumerable<Dealers> gradeData) { return gradeData .GroupBy(row => new { row.Name }) .Select(g => new Dealers { Name = g.Key.Name, TotalPoints =

How to pass lambda expressions as parameter in C#

不想你离开。 提交于 2021-02-04 19:09:04
问题 I am beginner at using lambda expressions. I have a list of dealers, foreach dealer I have to calculate grade. The request is that the grade calculation to be separated into a separate method. So I am writing the below two methods, however I am unable to pass parameters to CalculateGrade() method, public IEnumerable<Dealers> GetDealerGrades(IEnumerable<Dealers> gradeData) { return gradeData .GroupBy(row => new { row.Name }) .Select(g => new Dealers { Name = g.Key.Name, TotalPoints =

Check a List<List<int>[]> contains a value using Linq

核能气质少年 提交于 2021-02-04 16:42:22
问题 I have a List<List<int>[]> containing lists items e.g List<int> a= new List<int>(){ 2, 2, 3, 4, 5}; List<int> b= new List<int>() { 2, 2, 2, 6 }; List<List<int>[]> c = new List<List<int>[]>(); c.Add(new[]{a,b}); I want to check if any of the arrays contained in c have 2 as a value . This is more or less a true or false answer. So far I can check if a or b contains 2 using the Linq code var result = a.Any(p=> p==2); // this outputs 2 extending this to the c var result=c.Any(p=> p.Select(value =

Observing incoming websocket messages with Reactive Extensions?

自作多情 提交于 2021-02-04 16:09:14
问题 I want to use linq to process events received via a websocket connection. This is what I have so far: private static void Main() { string WsEndpoint = "wss://push.planetside2.com/streaming?environment=ps2&service-id=s:quicktesting"; using (WebSocket ws = new WebSocket(WsEndpoint)) { ws.OnMessage += Ws_OnMessage; ws.Connect(); Console.ReadKey(); ws.Close(); } } private static void Ws_OnMessage(object sender, MessageEventArgs e) { Console.WriteLine(e.Data); } The first think that stumps me is

Observing incoming websocket messages with Reactive Extensions?

那年仲夏 提交于 2021-02-04 16:08:36
问题 I want to use linq to process events received via a websocket connection. This is what I have so far: private static void Main() { string WsEndpoint = "wss://push.planetside2.com/streaming?environment=ps2&service-id=s:quicktesting"; using (WebSocket ws = new WebSocket(WsEndpoint)) { ws.OnMessage += Ws_OnMessage; ws.Connect(); Console.ReadKey(); ws.Close(); } } private static void Ws_OnMessage(object sender, MessageEventArgs e) { Console.WriteLine(e.Data); } The first think that stumps me is

Why is OrderBy which returns IOrderedEnumerable<T> much faster than Sort?

a 夏天 提交于 2021-02-04 14:30:14
问题 This is a follow up of this excellent question C# Sort and OrderBy comparison. I will use the same example: List<Person> persons = new List<Person>(); persons.Add(new Person("P005", "Janson")); persons.Add(new Person("P002", "Aravind")); persons.Add(new Person("P007", "Kazhal")); The methods in contention are: persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true)); //and persons.OrderBy(n => n.Name); Let me start by saying that I understand there isn't any significant performance

Why is OrderBy which returns IOrderedEnumerable<T> much faster than Sort?

♀尐吖头ヾ 提交于 2021-02-04 14:29:06
问题 This is a follow up of this excellent question C# Sort and OrderBy comparison. I will use the same example: List<Person> persons = new List<Person>(); persons.Add(new Person("P005", "Janson")); persons.Add(new Person("P002", "Aravind")); persons.Add(new Person("P007", "Kazhal")); The methods in contention are: persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true)); //and persons.OrderBy(n => n.Name); Let me start by saying that I understand there isn't any significant performance

Select all columns but group by only one in linq

会有一股神秘感。 提交于 2021-02-04 13:49:25
问题 I have been looking for a way to get multiple columns but group by only one in SQL and I found some info. However I can not came up with a way to do it in linq. I have the following toy example table: | Id | Message | GroupId | Date | |-------------------------------| | 1 | Hello | 1 | 1:00 | | 2 | Hello | 1 | 1:01 | | 3 | Hey | 2 | 2:00 | | 4 | Dude | 3 | 3:00 | | 5 | Dude | 3 | 3:01 | And I would like to recover all columns for the rows that have a distinct GroupId as follows (with a 'Date'

How can i write SQL update query with where clause in Entity Framework in C#

我的未来我决定 提交于 2021-02-04 12:41:08
问题 I know that for update a model in EF i must get model then change it then dbContext.SaveChanges() but sometimes in my Asp.net mvc project , i want update a field in my table and i don't know it 's id and must be get it with where clause. but i don't want connect twice to database , because in ADO.net i can write: UPDATE MyTable SET Field3 = "NewValue" WHERE Active = 1 and now i want write a linq to sql for EF that work like that. Is exist any way for that? thanks 回答1: You can't. EF is ORM,