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://learn.microsoft.com/en-us/dotnet/csharp/linq/index

86 questions
69
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
  • 45,951
  • 44
  • 147
  • 243
67
votes
8 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
  • 15,181
  • 13
  • 67
  • 106
60
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
  • 13,925
  • 11
  • 59
  • 92
42
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
  • 98,673
  • 67
  • 256
  • 322
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
  • 75,180
  • 37
  • 119
  • 173
20
votes
4 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
  • 11,087
  • 17
  • 62
  • 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
  • 49,248
  • 7
  • 89
  • 127
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
  • 41,281
  • 29
  • 127
  • 212
10
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
  • 4,243
  • 2
  • 27
  • 31
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,503
  • 3
  • 38
  • 76
5
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: [ …
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,682
  • 3
  • 25
  • 33
5
votes
2 answers

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

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…
ryanulit
  • 4,983
  • 6
  • 42
  • 66
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
  • 38,383
  • 7
  • 71
  • 92
4
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,621
  • 2
  • 18
  • 34
1
2 3 4 5 6