Questions tagged [linq-query-syntax]

Language-Integrated Query is the name for a set of technologies based on the integration of query capabilities directly into the C# language.

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on. With LINQ, a query is a first-class language construct, just like classes, methods, events.

Full Overview: https://docs.microsoft.com/en-us/dotnet/csharp/linq/index

88 questions
66
votes
7 answers

Extension methods syntax vs query syntax

I'm trying to get a handle on if there's a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, just are written differently. Is it purely a matter of style? var query = from p…
Armstrongest
  • 14,284
  • 13
  • 61
  • 102
58
votes
1 answer

LINQ - Query syntax vs method chains & lambda

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 syntax or a Lambda expression inside one of the LINQ extension methods? This applies to any Entities, SQL, objects,…
Connell
  • 12,845
  • 9
  • 53
  • 84
53
votes
3 answers

Return list of specific property of object using linq

Given a class like this: public class Stock { public Stock() {} public Guid StockID { get; set; } public string Description { get; set; } } Lets say I now have a List. If I would like to retrieve a list of all the StockIDs and…
Maxim Gershkovich
  • 42,124
  • 39
  • 134
  • 227
37
votes
1 answer

LINQ: dot notation equivalent for JOIN

Consider this LINQ expression written using query notation: List pr = (from p in db.Persons join e in db.PersonExceptions on p.ID equals e.PersonID where e.CreatedOn >=…
p.campbell
  • 91,713
  • 61
  • 243
  • 314
20
votes
4 answers

GroupBy with linq method syntax (not query syntax)

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…
Dane O'Connor
  • 67,996
  • 36
  • 114
  • 164
17
votes
3 answers

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

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…
Mike Barlow - BarDev
  • 10,577
  • 16
  • 59
  • 83
13
votes
2 answers

Inconsistency in C# spec 7.16.2.5

I'm having a go at implementing C# spec 7.16.2 "Query expression translation" in Roslyn. However, I've run into a problem in 7.16.2.5 "Select clauses". It reads A query expression of the form from x in e select v is translated into ( e ) . Select…
Rawling
  • 45,907
  • 6
  • 80
  • 115
12
votes
6 answers

Parse string into a LINQ query

What method would be considered best practice for parsing a LINQ string into a query? Or in other words, what approach makes the most sense to convert: string query = @"from element in source where element.Property = ""param"" …
smartcaveman
  • 38,142
  • 26
  • 119
  • 203
9
votes
1 answer

LINQ - Method vs Query Syntax Difference

I was working with a DataTable and noticed that Resharper recommended that I can convert a loop into a LINQ expression. I did so and it was rewritten in query expression syntax (simplified): var test1 = from DataRow row in dt.Rows select…
Lester
  • 3,973
  • 2
  • 24
  • 28
6
votes
3 answers

How to use query syntax to create permutations?

I've tried to write a method which returns the permutation of a given enumerable as simple as possible. The code: using System.Collections.Generic; public static partial class Permutable { static IEnumerable>…
Ken Kin
  • 4,165
  • 3
  • 34
  • 71
5
votes
1 answer

Join in LINQ Query Syntax: moving right side to the left

I have a scenario where I need to join two tables: A |---------------------|------------------| | ID | Name | |---------------------|------------------| | 1 | John …
Jessica
  • 1,441
  • 1
  • 14
  • 27
5
votes
2 answers

Why does this LINQ query compile?

After reading "Odd query expressions" by Jon Skeet, I tried the code below. I expected the LINQ query at the end to translate to int query = proxy.Where(x => x).Select(x => x); which does not compile because Where returns an int. The code compiled…
mcrumley
  • 5,552
  • 3
  • 23
  • 31
5
votes
2 answers

Convert EF LINQ method syntax to query syntax

How would you write this exact same query using the LINQ query syntax? var q2 = list.GroupBy(x => x.GroupId) .Select(g => g .OrderByDescending(x => x.Date) .FirstOrDefault()); I though this should work…
Eren Ersönmez
  • 36,276
  • 7
  • 63
  • 88
4
votes
1 answer

Joining tables in EF Core Linq Query

I'm currently trying to make a Web Api with EF Core, and i'm running into some problems joining the tables i've got together. I'm working with the following Database Diagram: And the data i'm currently getting back from my API looks this this: [ …
4
votes
3 answers

Which is fast : Query Syntax vs. Loops

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> …
Nawaz
  • 327,095
  • 105
  • 629
  • 812
1
2 3 4 5 6