• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Programming / How To Work With LinQ C# Lambda Expression

How To Work With LinQ C# Lambda Expression

April 7, 2013 by Rana Ahsan 5 Comments

linq to xml tutorial

If you are using LinQ as ORM and getting your staffs done with simple object oriented way, that’s nice! Have you seen some one liner magic code which don’t look much familiar to our usual C# code? Those are lambda expressions, a cool way to get things done in simple and easy way. In today’s tutorial We will discuss about basic understanding of C# lambda expression, which play a very important role in .NET’s LinQ, a very easy,efficient and effective technology to handle data.

If you are very new and don’t have any knowledge on linq yet, you may consider reading my other post about beginners guide to linq

What Is Lambda Expressions?

(Input Parameters) => Method Expression

don’t get mad just by having a glance at a lambda expression statement, It’s not an alien Language. It’s simply a Function(or delegate)! Well, we can call it as anonymous function. It’s a function without a name. But it has parameter and return value, but they are dynamic, which means we can put anything for them, its on us. Wow! How flexible. Yes, it is.

labmda expression

However, you will have to remember one thing though, as soon as we use it in code, wherever that is, it’s scope finishes right there, no way to reuse it anymore, as there is to name assigned to it! So, mostly, we will use this on some tasks which will need some computation and we want to write it easy and efficient way, but don’t need to reuse it again often. If we need so, this must be assigned to a delegate or ‘Func’ type.

General Usage Of C# Lambda Expression:

In general lambda expression is handy while using LinQ query in your application an apply them on a list of data, either from database, from XML or dynamic. In other words, we usual use it to filter out some specific data from a list of data that meets the criteria and use them on a LinQ operation.lambda expression can be used in wide range of area like linq to sql, linq to xml, linq to entity(Entity framework), array/list data etc.

Read All LinQ Tutorials By CodeSamplez.com

A Simple Example:

Please take a look at the following expression:

x => x >10

This is a very simple example how a lambda expression can be. The first part, before the ‘=>’ symbol, is refereed as the input parameter. It doesn’t need to be defined previously, its dynamic. It decides the type intelligently, even in compile time, when you are writing it, based on the operation being performed on it.

As we said, we want to filter out some specific data from a list of data, this input parameter stands for the type of each element of the list. Lets say, we are dealing with a list of integers data, then x is an integer. If we are dealing with a list of ‘A’ object, then x is also be of ‘A’ type and you will get full support of visual studio intelligent for it.

So lets see the complete statement of the above expression:

int[] somevalues = { 10, 20, 5, 2, 40, 1 };
int numberOfExpectedValues = somevalues.Count(x => x >10);

As you can see, the lambda expression is returning value which are being applied on ‘Count’ method of ‘somevalues’ array of integers. So, I mentioned, now x will be treated as an integer. And the expression will return a true/false value with testing each element of ‘somevalues’ array and will be used as delegate of count method. finally, in the ‘numberOfExpectedValues’ variable we will receive how many data meet the criteria of being greater than 10.

Another Pseudo Code Example:

How we can use it on a list of objects and how handy that would be? Lets see a pseudo code in c#(you have to write few more code to make it compile):

List<User> myUsers = New List<User>();
//fill the list myUsers with some User object
List<User> activeUsers = myUsers.Where(u => u.Active == True);
//the activeUsers list will contain only active users filtered from list of all users 

So, without using any ugly SQL query or few lines of codes for a function to do this manually, we are having this in a single line of handy code. Isn’t it amazing?

References:

Checkout msdn documentation on labmda expression as further resource. Hope this small tutorial on linq c# lambda expression will help you in your way further.

Please let me know if you are having any issue in understanding any part of the tutorial. Keep in touch. Happy coding 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Programming Tagged With: c#, linq

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. Muhammed Shafi K says

    September 17, 2014 at 1:35 am

    Nice!!

    Reply
  2. Deva says

    May 2, 2015 at 7:30 am

    nice !!! thank u

    Reply
  3. Hummayun says

    May 20, 2015 at 8:38 am

    Good work… Jazakallah…

    Reply
  4. Mostafizur Rahman (@mostafiz57) says

    June 12, 2015 at 1:57 pm

    Nice and clear description , Thanks @Rana.

    Reply
  5. ravi says

    July 1, 2015 at 1:05 am

    please change the font color of Pseudo Code

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Beginning Codeigniter Application Development
    Beginning Codeigniter Application Development
  • Control HTML5 Audio With Jquery
    Control HTML5 Audio With Jquery
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023