0% found this document useful (0 votes)
106 views10 pages

Dynamic LINQ: Pankaj Kumar Gupta Senior Software Engineer Miri Infotech (P) LTD, India

This document discusses Dynamic LINQ, which allows querying objects using string-based expressions. It provides extension methods to query data contexts dynamically. These methods can construct complex queries with filtering, ordering, paging and joins. It also describes the ParseException class to handle errors parsing dynamic query strings.

Uploaded by

goldryan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views10 pages

Dynamic LINQ: Pankaj Kumar Gupta Senior Software Engineer Miri Infotech (P) LTD, India

This document discusses Dynamic LINQ, which allows querying objects using string-based expressions. It provides extension methods to query data contexts dynamically. These methods can construct complex queries with filtering, ordering, paging and joins. It also describes the ParseException class to handle errors parsing dynamic query strings.

Uploaded by

goldryan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Dynamic LINQ

PANKAJ KUMAR GUPTA Senior Software Engineer Miri Infotech (P) Ltd, India Click to edit Master subtitle style

4/18/12

LINQ to SQL is an extension of LINQ that allows developers to write "queries" in .NET to retrieve and manipulate data from a SQL Server database. This gives the ability to access relational database objects as normal .Net objects. LINQ to SQL integrates SQL based schema definitions to the common language runtime (CLR) type system. 4/18/12 22 This provides strong typing, syntax

Normally LINQ queries constructed by using language operators or typesafe lambda extension methods. but the dynamic query library provides us with string based extension methods, where we will pass the expressions in string format. The Dynamic Expression API is present in the System.Linq.Dynamic namespace. This API provides classes:
33

4/18/12

The Dynamic Expression API provides the following IQueryable Extension Methods as part of System.Linq.Dynamic.DynamicQuery able class for dynamically querying objects.

public static IQueryable Where(this IQueryable source, string predicate, params object[] values); public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values); 4/18/12 44

Constructing Dynamic Queries using IQueryable extension Methods DataClassesDataContext db=new DataClassesDataContext(); var products = from p in db.Products where p.Model == "SD1000" && p.onsale==true select p;
4/18/12 55

DataClassesDataContext db=new DataClassesDataContext(); var product = from p in db.Products where p.Model == "SD1000" && p.onsale == true orderby p.Name select new { p.ItemId, p.Name, p.Model1, p.onsale };

That can be converted like below:


4/18/12

DataClassesDataContext db = new

66

The ParseException Class


Normally when constructing dynamic queries by concatenating strings to form an expression string there is a chance of getting parsing errors. The Dynamic Expression API provides ParseException class which can be used to catch the parsing errors. The Position property gives the character index in the expression string at which the parsing error occurred 77 and the 4/18/12

The Take() extension method is used to select a no of rows from the query result and Skip() extension method is used to skip no of rows from the query result.

DataClassesDataContext db=new DataClassesDataContext(); string condition = "Model==\"SD1000\" && onsale==true "; var product2 = 4/18/12 88

JOINS in LINQ

There is no direct support for Joins using the extension methods provided in the Dynamic Expression API. However one can use joins indirectly by using inner queries, where the inner queries can be constructed using Dynamic Expression API like below.
4/18/12

DataClassesDataContext db=new

99

Thank You Queries?

4/18/12

1010

You might also like