CodeSamplez.com

Programming, Web development, Cloud Technologies

  • Facebook
  • Google+
  • RSS
  • Twitter
  • 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
Home Programming How To Work With LinQ C# Lambda Expression

How To Work With LinQ C# Lambda Expression

Rana Ahsan April 7, 2013 5 Comments


 How To Work With LinQ C# Lambda Expression    

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 🙂

Related

Filed Under: Programming Tagged With: c#, linq

About Rana Ahsan

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

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.

Email Subscription

Never miss any programming tutorial again.

Popular Tutorials

  • How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
  • Generate HTTP Requests using c#
  • Utilizing Config File In C#.NET Application
  • How To Work With Codeigniter Caching In PHP
  • Using Supervisord Web Interface And Plugin
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
  • LinQ To SQL Database Update Operations In C#

Recent Tutorials

  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read
  • Getting Started With Golang Unit Testing
  • Getting Started With Big Data Analytics Pipeline
  • NodeJS Tips And Tricks For Beginners
  • Apple Push Notification Backend In NodeJS
  • Web Based Universal Language Translator, Voice/Text Messaging App
  • How To Dockerize A Multi-Container App From Scratch

Recent Comments

  • S. Chalisque on PHP HTML5 Video Streaming Tutorial
  • Armorik on Generate HTTP Requests using c#
  • iswaps on PHP HTML5 Video Streaming Tutorial
  • TAKONDWA on PHP HTML5 Video Streaming Tutorial
  • rorenzo on PHP HTML5 Video Streaming Tutorial

Archives

Resources

  • CodeSamplez.com Demo

Tags

.net apache api audio aws c# cache cloud server codeigniter deployment doctrine facebook git github golang htaccess html5 http image java javascript linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty ssh tfs thread tips ubuntu unit-test utility web application wordpress wpf

Copyright © 2010 - 2022 · CodeSamplez.com ·

Copyright © 2022 · Streamline Pro Theme on Genesis Framework · WordPress · Log in