Dynamic Query in LINQ using Predicate Builder


This tutorial explains how to create a dynamic query using LINQ, Using Predicate Builder  LINQ to SQL dynamic query and Query with Entity Framework is easy. This concept first implement by albahari


Predicate Builder is powerful LINQ expression which is mainly used when too many search filter parameter is there for querying data by writing dynamic query expression. We can write a query like Dynamic SQL.

To know more about predicate delegate visit Predicate Delegate

How To Implement Predicate Builder

In this example, I have created an instance of PredicateBuilder with PatientInfo Model and add multiple OR and AND Condition based on their value. Predicate Builder automatically creates a dynamic query with Linq and combined into one Expression.


When We have a grid which filters record based on applied filter and filter parameter is in large number decision of use Dynamic LINQ result very high performance and minimize code writing while implementation otherwise it requires a number of if else statement based on filter parameter.

In the above code, I have used entity framework Entity function which is very useful when we have to perform DateTime operation. It is internally worked as SQL DateTime function.



Above Predicate builder helper method reference from albahari and from StackOverflow article and learn how powerful feature it is. predicate builder also works with IEnumerable and IQueryable.


This article explains the implementation of Dynamic Linq query using Predicate Builder. Hope this article is useful while implementing dynamic query using LINQ.
Thanks to Pavel Valdov for C# code syntax highlighter.

Connect to TFS using CSharp


In this article, we learn how to connect TFS (Team Foundation Server) or VSO (Visual Studio Online) using C#(csharp). We can create console application based on TFS API.



Before starting to working on authenticating TFS using C#, we required TFS Client library reference in the project. You can add the required library using a command in NuGet Package Manager.

Install-Package Microsoft.TeamFoundationServer.Client -Version 14.95.3
Install-Package Microsoft.VisualStudio.Services.Client -Version 14.95.3
After adding reference library you can copy below code in your app and add a required reference in the project.

Source Code:

 NetworkCredential credential = new NetworkCredential("username", "password");
 BasicAuthCredential basicCred = new BasicAuthCredential(credential);
 TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
                    new Uri("https://project.visualstudio.com/DefaultCollection"),

As per above code, we require valid username and password which is passed to NetworkCredential instance and also require valid TFS URL which you are trying to connect using C#.

We can authenticate a user with basic authentication mechanism without prompting for TFS login dialog, that’s why we have passed credential as an argument in a BasicAuthCredential object. Without basic authentication mechanism app prompt for login information.

TfsTeamProjectCollection instance accept two arguments 1) Default collection Url 2) Credential with basic authentication wrapper. 

TfsTeamProjectCollection instance has authenticate() method which actually requests for authentication and returns result or exception based on the credential.


In this article, we have explored Basic TFS API and authenticated TFS without prompt for username and password.

Login using Google API with C#


In this article, we learn about how to login using Google OAuth 2.0 with C#(.NET) or Authenticate Google API in Dot Net.


As we know Google OAuth 2.0 requires ClientId and ClientSecret before we proceeding to further step. So, first of all, we have to generate ClientId and ClientSecret from Google Developer Console by creating credential or generating a credential for Web Application or from Native Application.

I hope you have google account already if not then first create one before proceeding further.

Step 1: Create Credential for OAuth App.
Select credential type OAuth ClientID
Step 2: Select appropriate application type from available option.
Select application type

Based on where we are using login with google account we can select an application type. Here I have use other which works for a console app, Web application hosted on localhost as well as on domain.

Step 3: Copy and save generate ClientId and Client Secret
Save ClientId and Client Secret

Now, you have your ClientId and ClientSecret which will be used for authenticating with Google API using C#.

Once you have setup project you need Google API Reference you can install from Nuget


Install-Package Google.Apis -Version 1.32.1

Here is main logic for authenticating google API
public async Task<Usercredential> getUserCredential()
            UserCredential credential;
            string[] scopes = new string[] {  }; // view and manage your Google Analytics data

            //Read client id and client secret from json file

            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                       new ClientSecrets
                           ClientId = ConfigurationManager.AppSettings["ClientId"],
                           ClientSecret = ConfigurationManager.AppSettings["ClientSecret"]
                       }, scopes,
                "user", CancellationToken.None, new FileDataStore("Auth.Api.Store"));

            return credential;
Hi, here I have created one single method

  • Which grabs ClientId and Client Secret from Web.Config App settings.
  • Passes scopes as a parameter. You can request more detail if you want while requesting from the user
  • Used await and task feature for thread safe and as performance improvement and it is already supported by Google API Library.
When you put above code in your project and run the app it will redirect to Google Account page for validating your credential and request for granting permission to use your basic profile information for login purpose.

If you receive any error it means permission is not given or ClientId or Client Secret key is not valid.


In this article, we have learned about how to create ClientId and Client Secret for a talk with Google API and Login with Google Account functionality using C#.Code.

Dynamic Query in LINQ using Predicate Builder

Introduction This tutorial explains how to create a dynamic query using LINQ, Using Predicate Builder  LINQ to SQL dynamic query an...

Learn More