linq

EF Core 3.0 - Convert SQL to LINQ

放肆的年华 提交于 2021-01-27 10:49:19
问题 The example given in the blog has the following from e in s.StudentCourseEnrollments where courseIDs.Contains(e.Course.CourseID) select e The contains logic will not work when we are looking for an exact match. If a student has enrolled for 6 courses (ex : 1,2,3,4,5,6) and the requested list contains 5 (ex: 1,2,3,4,5) the query will return a match when it should not. The other way works well when the student has enrolled in a subset of the requested list. Below solution works but need help to

EF Core 3.0 - Convert SQL to LINQ

て烟熏妆下的殇ゞ 提交于 2021-01-27 10:48:29
问题 The example given in the blog has the following from e in s.StudentCourseEnrollments where courseIDs.Contains(e.Course.CourseID) select e The contains logic will not work when we are looking for an exact match. If a student has enrolled for 6 courses (ex : 1,2,3,4,5,6) and the requested list contains 5 (ex: 1,2,3,4,5) the query will return a match when it should not. The other way works well when the student has enrolled in a subset of the requested list. Below solution works but need help to

How to group records and retrieve only first group with top N records

我只是一个虾纸丫 提交于 2021-01-27 08:23:11
问题 I have the following record set ID BatchID ClientName CreatedDateTime ----------- -------------- --------------- ----------------------- 1 NULL B 2018-02-16 19:07:46.320 2 NULL B 2018-02-16 19:07:46.320 3 NULL B 2018-02-16 19:07:46.597 4 NULL B 2018-02-16 19:07:46.597 5 NULL B 2018-02-16 19:10:10.260 6 NULL B 2018-02-16 19:10:10.260 7 NULL B 2018-02-16 19:21:34.303 8 NULL B 2018-02-16 19:21:34.303 9 NULL B 2018-02-16 19:21:44.780 10 NULL B 2018-02-16 19:21:44.780 11 NULL A 2018-02-16 19:24:35

How to group records and retrieve only first group with top N records

耗尽温柔 提交于 2021-01-27 08:20:37
问题 I have the following record set ID BatchID ClientName CreatedDateTime ----------- -------------- --------------- ----------------------- 1 NULL B 2018-02-16 19:07:46.320 2 NULL B 2018-02-16 19:07:46.320 3 NULL B 2018-02-16 19:07:46.597 4 NULL B 2018-02-16 19:07:46.597 5 NULL B 2018-02-16 19:10:10.260 6 NULL B 2018-02-16 19:10:10.260 7 NULL B 2018-02-16 19:21:34.303 8 NULL B 2018-02-16 19:21:34.303 9 NULL B 2018-02-16 19:21:44.780 10 NULL B 2018-02-16 19:21:44.780 11 NULL A 2018-02-16 19:24:35

Find item from generic list

蓝咒 提交于 2021-01-27 07:43:40
问题 I have a problem in fetching the record from a generic list. I have created a common function from where i want to get the records from any type of class. Below is sample code:- public void Test<T>(List<T> rEntity) where T : class { object id = 1; var result = rEntity.Where(x => x.id == id); } Please suggest. Thanks in advance. 回答1: With method like that a usual question for compiler is 'what is T' ? If it's just a class it could be anything even a StringBuilder as Jon has mentioned and there

Find item from generic list

旧巷老猫 提交于 2021-01-27 07:43:39
问题 I have a problem in fetching the record from a generic list. I have created a common function from where i want to get the records from any type of class. Below is sample code:- public void Test<T>(List<T> rEntity) where T : class { object id = 1; var result = rEntity.Where(x => x.id == id); } Please suggest. Thanks in advance. 回答1: With method like that a usual question for compiler is 'what is T' ? If it's just a class it could be anything even a StringBuilder as Jon has mentioned and there

Multiple enumeration and use of Any()

我是研究僧i 提交于 2021-01-27 07:27:16
问题 I'm trying to figure out what would be the proper convention for LINQ when I need to do something like the following If there items, print them line-by-line If there are no items, print "No items" The way I would think to do it is like if (items.Any()) { foreach (string item in items) { Console.WriteLine(item); } } else { Console.WriteLine("No items"); } However, that would technically violate the principle of multiple enumeration. A way to not violate that would be bool any = false; foreach

Multiple enumeration and use of Any()

房东的猫 提交于 2021-01-27 07:24:09
问题 I'm trying to figure out what would be the proper convention for LINQ when I need to do something like the following If there items, print them line-by-line If there are no items, print "No items" The way I would think to do it is like if (items.Any()) { foreach (string item in items) { Console.WriteLine(item); } } else { Console.WriteLine("No items"); } However, that would technically violate the principle of multiple enumeration. A way to not violate that would be bool any = false; foreach

Find max value of two (or more) properties in list

て烟熏妆下的殇ゞ 提交于 2021-01-27 05:59:17
问题 This question has been asked in one or the other way on SO but not like this. I just came over a very basic issue where I was looking for a statisfying solution :-) I got a list of objects which have two integer properties. Now I want to find the max value of both properties of all object in the list. I came up with three solutions: First approach: int max = Math.Max(list.Max(elem => elem.Nr), list.Max(elem => elem.OtherNr)); Second approach: public int Max(List<Thing> list) { int maxNr = 0;

Find max value of two (or more) properties in list

感情迁移 提交于 2021-01-27 05:58:50
问题 This question has been asked in one or the other way on SO but not like this. I just came over a very basic issue where I was looking for a statisfying solution :-) I got a list of objects which have two integer properties. Now I want to find the max value of both properties of all object in the list. I came up with three solutions: First approach: int max = Math.Max(list.Max(elem => elem.Nr), list.Max(elem => elem.OtherNr)); Second approach: public int Max(List<Thing> list) { int maxNr = 0;