LINQ & Entity Framework IQ's
Neither is better: they serve different needs. Query syntax comes into its own when you want to leverage multiple range variables. This happens in three situations:
- When using the let keyword
- When you have multiple generators (from clauses)
- When doing joins
How to select top 5 records from List using LINQ in C#?
var result = (from m in Names select m).Take(5);
How to select unique names from a List using LINQ in C#?
var result = (from m in Names select m).Distinct().ToList();
Get Last n Records using Linq to SQL
var qry = db.ObjectCollection
.Where(m => m.<field> == data)
.OrderByDescending(m => m.<field>)
.Take(n);
To get the last record for the collection you make use of FirstOrDefault function as below
var qry = db.ObjectCollection .Where(m => m.<field> == data)
.OrderByDescending(m => m.<field>)
.FirstOrDefault();
How to Remove Duplicates and Get Distinct records from List using LINQ?
var DistinctItems = employees.GroupBy(x => x.EmpID).Select(y => y.First());
How to use SQL like Operator in LINQ and C#?
var data = (from m in snippets where m.Name.Contains("code2") select m);
How to Get the Last Element from LINQ Query in C#?
string NewData = (from m in ABundantCodeVersions select m).Last();
How to Get the First Element from LINQ Query in C#?
string NewData = (from m in ABundantCodeVersions select m).First();
How to check if List is Empty using LINQ in C#?
//using Any() method
var ExistsAny = employees.Any(a => a.EmpID == 1);
//using count method
var ExistsCount = employees.Count(a => a.EmpID == 1)>0;
LINQ QUERIES:-
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query = names
.Where (n => n.Contains ("a"))
.OrderBy (n => n.Length)
.Select (n => n.ToUpper());
// The same query constructed progressively:
IEnumerable<string> filtered = names.Where (n => n.Contains ("a"));
IEnumerable<string> sorted = filtered.OrderBy (n => n.Length);
IEnumerable<string> finalQuery = sorted.Select (n => n.ToUpper());
No comments:
Post a Comment