Understanding LINQ in One-Liners

Here I will provide one liner-explanations of terminology used with LINQ.

  • From: initialize a data source just like we declare a variable first before using it in C#.
  • where: applies a condition in a LINQ query.
  • select: selects a value from the LINQ query.
  • var: assigns a variable for when we don't know the return type of our LINQ query.
  • Deffered execution: the LINQ query will store only the information, it will not provide the result directly to you. To get the result you need to use a foreach loop or toList or ToArray method. Until this stage LINQ is in the deffered execution stage.
  • foreach loop: to iterate the result that we got from the LINQ query and then to execute the result.
  • IEnumerable<string>: use this if you know that your LINQ query return type will be a string and the data is in your in-memory.
  • IQueryable<string>: use this if the query is generated using a database.
  • Lambda expression: use the => operator if you are using a method-based LINQ query. The left hand of the => operator is the input and the right hand side is the output.
  • Expression lambda: use an expression lambda if you want to pass an expression on the left hand side of the => operator.
  • Statement lambda: If you want to use a statement in the right hand side of the => operator then use the statement lambda enclosed in brackets {}.
  • Async Lambda: use this if you want to use async or await in your LINQ query.
  • Async and await: to do asynchronous programming like if one resource is busy then the other program will keep executing, it will not wait for completion of the resource task.
  • let: use the let keyword when you want to store the result of any expression inside the LINQ.
  • LINQ to SQL: use this if you want to access a collection from a SQL Server in your in-memory.
  • System.Data.Linq.dll: add this DLL if you want LINQ to SQL.
  • insertonsubmit: use this when you want to insert data into SQL using LINQ to SQL.
  • deleteonsubmit: use this when you want to delete data in SQL using LINQ to SQL.
  • submitchanges: use this in LINQ to SQL after insertonsubmit or deleteonsubmit or if you have updated any data in your in-memory collection. This will submit your data finally to the database.
  • LINQ to dataset: use this if you want to query your in-memory dataset.
  • System.Data.DataSetExtensions: use this DLL if you want to use LINQ to a dataset.
  • asenumerable: use this if you want to apply LINQ to a datatable since a datatable doesn't implement ienumerable or iqueryable.
  • field: use the field method in LINQ to access a datatable column using LINQ to dataset.
  • setfield: use SetField if you want to set column values in a DataRow using LINQ to dataset.
  • isnull: use isnull if you want to check that a column is null or not using LINQ to dataset.
  • join: use this if you want to join two datatables using LINQ to dataset.
  • groupjoin: use this when you want a superset of inner joins and left outer joins.
  • LINQ to Objects: use this if you want to apply the LINQ on an array, list or dictionary.
  • LINQ to XML: use this if you want to do manipulation with XML or if you want to create new XML using LINQ.
  • Distinct: use this with your LINQ query if you want to remove duplicate values from a collection.
  • Except: use this with LINQ if you want to return the set difference of two collections.
  • Intersect: use this if you want to return common data from two collections in LINQ.
  • Union: use this when you want all elements from both collections but the result should not contain duplicate data.
  • All: use this in LINQ if you want to check that your condition is satisfying all the elements of a collection.
  • Any: use this in LINQ if you want to check your condition with any element in a collection.
  • Contains: use this if you want to check whether or not a sequence contains a specified element.
  • Skip: use this if you want to skip elements up to a specified position in a sequence.
  • SkipWhile: use this if you want to skip elements until the condition is true.
  • Take: use this if you want to take the first n elements from a sequence.
  • Takewhile: use this if want to take all the elements when the condition becomes true.
  • concat: use this if you want to combine two sequences, it may contain duplicate values also.
  • AsQueryable: use this if you want to convert a generic Ienumerable type to a generic IQueryable Type.
  • Cast: use this if you want to cast an element of a sequence in any other type.
  • ToArray: use this if you want to convert your collection into an array and if you want to force query execution.
  • ToDictionary: use this function if you want to put your element into a dictionary based on key. This will also force query execution.
  • ToList: use this if you want to convert collections into a generic list. This will also force query execution.
  • ToLookup: this is also like a dictionary but with a one-to-many relationship. Use this if you want to put your element into a dictionary.


Similar Articles