linq-query-syntax

GroupBy with linq method syntax (not query syntax)

别说谁变了你拦得住时间么 提交于 2020-04-29 10:22:10
问题 How would the following query look if I was using the extension method syntax? var query = from c in checks group c by string.Format("{0} - {1}", c.CustomerId, c.CustomerName) into customerGroups select new { Customer = customerGroups.Key, Payments = customerGroups } 回答1: It would look like this: var query = checks .GroupBy(c => string.Format("{0} - {1}", c.CustomerId, c.CustomerName)) .Select (g => new { Customer = g.Key, Payments = g }); 回答2: First, the basic answer: var query = checks

Which is fast : Query Syntax vs. Loops

人走茶凉 提交于 2020-01-01 11:49:14
问题 The following code provides two approaches that generate pairs of integers whose sum is less than 100, and they're arranged in descending order based on their distance from (0,0). //approach 1 private static IEnumerable<Tuple<int,int>> ProduceIndices3() { var storage = new List<Tuple<int, int>>(); for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { if (x + y < 100) storage.Add(Tuple.Create(x, y)); } } storage.Sort((p1,p2) => (p2.Item1 * p2.Item1 + p2.Item2 * p2.Item2).CompareTo(

Does LINQ “Query Syntax” Support Duck Typing?

倾然丶 夕夏残阳落幕 提交于 2019-12-19 11:55:34
问题 Regarding LINQ query syntax... var foo = new List<int> { 1, 2 }; var boo = from n in foo where n > 1 select n; ...I always thought this syntax was limited to operating on IEnumerable. Or at least until I learned about IQueryable. And perhaps IObservable as well. But I recently noticed a suggestion that query syntax is based on duck typing. That story didn't look terribly convincing, until I found a site that is dedicated to LINQ to Tasks. LINQ to Tasks looks like it is wholly dependent on

How to Convert LINQ Comprehension Query Syntax to Method Syntax using Lambda

我们两清 提交于 2019-12-17 16:07:36
问题 Is there a tool, process or a solution that will convert the following LINQ Query Syntax to Method Syntax with Lambdas (dot notation)? I would expect the solution to convert the following Query Syntax to a Method Syntax such as this. var filteredEmployees = from employee in allEmployees where employee.DepartmentID < 4 && employee.EmployeeID < 10 orderby employee.DepartmentID descending, employee.LastName descending select employee; To the following var filteredEmployees2 = allEmployees.Where

LINQ - Query syntax vs method chains & lambda [closed]

丶灬走出姿态 提交于 2019-12-17 07:12:17
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Does anyone stick to any rules (or are you forced to stick to any rules by your employer?) when choosing to use either LINQ query

How to create Query syntax for multiple DataTable for implementing IN operator of Sql Server

荒凉一梦 提交于 2019-12-11 08:47:17
问题 I have fetched 3-4 tables by executing my stored procedure. Now they resides on my dataset. I have to maintain this dataset for multiple forms and I am not doing any DML operation on this dataset. Now this dataset contains 4 tables out of which i have to fetch some records to display data. Data stored in tables are in form of one to many relationship. i.e. In case of transactions. N records per record. Then these N records are further mapped to M records of 3rd table. Table 1 MAP_ID GUEST_ID

Force inner join with many-to-many relationship entity framework

安稳与你 提交于 2019-12-10 15:32:25
问题 I have a many-to-many relationship setup in my database like so: User ------- Id (PK, Identity) First Last ...various other fields Skill ------- Id (PK, Identity) Description UserSkill ----------- UserId (PK, FK on User.Id) SkillId (PK, FK On Skill.Id) When I run this LINQ query on the DbContext: from u in Users from s in u.Skills where s.Id == 5 select new { u.Id, s.Description }) The SQL generated contains all inner joins which is what I want: SELECT [Extent1].[UserId] AS [UserId], [Extent2

Which is fast : Query Syntax vs. Loops

与世无争的帅哥 提交于 2019-12-04 09:08:32
The following code provides two approaches that generate pairs of integers whose sum is less than 100, and they're arranged in descending order based on their distance from (0,0). //approach 1 private static IEnumerable<Tuple<int,int>> ProduceIndices3() { var storage = new List<Tuple<int, int>>(); for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { if (x + y < 100) storage.Add(Tuple.Create(x, y)); } } storage.Sort((p1,p2) => (p2.Item1 * p2.Item1 + p2.Item2 * p2.Item2).CompareTo( p1.Item1 * p1.Item1 + p1.Item2 * p1.Item2)); return storage; } //approach 2 private static IEnumerable

LINQ: dot notation equivalent for JOIN

断了今生、忘了曾经 提交于 2019-12-03 03:03:09
问题 Consider this LINQ expression written using query notation: List<Person> pr = (from p in db.Persons join e in db.PersonExceptions on p.ID equals e.PersonID where e.CreatedOn >= fromDate orderby e.CreatedOn descending select p) .ToList(); Question: how would you write this LINQ expression using dot notation? 回答1: Like this: List<Person> pr = db.Persons .Join(db.PersonExceptions, p => p.ID, e => e.PersonID, (p, e) => new { p, e }) .Where(z => z.e.CreatedOn >= fromDate) .OrderByDescending(z => z

LINQ: dot notation equivalent for JOIN

血红的双手。 提交于 2019-12-02 16:36:44
Consider this LINQ expression written using query notation: List<Person> pr = (from p in db.Persons join e in db.PersonExceptions on p.ID equals e.PersonID where e.CreatedOn >= fromDate orderby e.CreatedOn descending select p) .ToList(); Question: how would you write this LINQ expression using dot notation? Like this: List<Person> pr = db.Persons .Join(db.PersonExceptions, p => p.ID, e => e.PersonID, (p, e) => new { p, e }) .Where(z => z.e.CreatedOn >= fromDate) .OrderByDescending(z => z.e.CreatedOn) .Select(z => z.p) .ToList(); Note how a new anonymous type is introduced to carry both the p