Skip to main content

Chapter 6.4: Working with .NET Classes: Understanding System.Linq

 Introduction:

In the previous article, Chapter 6.3, we discussed the System.Collections and System.Collections.Generic namespaces, which provide classes for working with collections of objects in C# applications. In this final article of our series, we'll explore the System.Linq namespace, which offers powerful querying capabilities for collections, allowing you to filter, sort, and transform data with ease. After completing this article, you should have a solid understanding of C# and its core concepts. We encourage you to start a project to implement what you've learned and further strengthen your skills.


System.Linq Namespace:

The System.Linq namespace provides Language Integrated Query (LINQ) functionality, which allows you to perform complex queries on collections using a concise and expressive syntax. LINQ works with both generic and non-generic collections, as well as with other data sources like XML and relational databases. Some key features of LINQ include:

  • Filtering: Select items from a collection that match a specified condition.
  • Projection: Transform items in a collection into a different form.
  • Ordering: Sort items in a collection based on one or more criteria.
  • Grouping: Group items in a collection by a specified key.
  • Aggregation: Calculate summary information about a collection, such as the sum or average of its elements.

Basic LINQ Queries:

Here are some examples of basic LINQ queries using the System.Linq namespace:

using System.Linq;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// Filter even numbers
var evenNumbers = numbers.Where(n => n % 2 == 0);

// Project numbers to their squares
var squares = numbers.Select(n => n * n);

// Sort numbers in descending order
var sortedNumbers = numbers.OrderByDescending(n => n);

// Group numbers by even/odd
var groupedNumbers = numbers.GroupBy(n => n % 2 == 0 ? "Even" : "Odd");


Query Syntax vs Method Syntax:

LINQ provides two syntax styles for writing queries: query syntax and method syntax. Query syntax resembles SQL and is more declarative, while method syntax uses extension methods and lambda expressions:

// Query syntax
var evenNumbers = from n in numbers
                  where n % 2 == 0
                  select n;

// Method syntax
var evenNumbers = numbers.Where(n => n % 2 == 0);

Both syntax styles are interchangeable and can be used according to your preference.


Conclusion:

In this final article of our series, we explored the System.Linq namespace and its powerful querying capabilities for collections. With the knowledge you've gained from this series, you should have a solid understanding of C# and its core concepts. Now it's time to put your skills into practice! Start a project to implement what you've learned and continue building your expertise in C#. Happy coding!

Check out the Sample project here.

Comments

Some of My Bests

ডাটা স্ট্রাকচার- স্ট্যাক (Stack)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা স্ট্যাক বুঝানোর জন্য। স্ট্যাক খুবই ইন্টেরেস্টিং একটা ডাটা স্ট্রাকচার। অনেক কারনেই এটা ব্যাবহার হয়। আগে স্ট্যাক কনসেপ্টটা নিয়ে আলোচনা করা যাক। স্ট্যাক বলতে বোঝায় একটার উপর একটা সাজায়ে রাখা। বিয়ের বাড়িতে আগে মেলামাইনের গ্লাস দেখা যেত একটার ভেতর আরেকটা ঢুকায়ে লম্বা একটা পাইল তৈরি করে একসাথে ক্যারি করা হচ্ছে। এটা একটা স্ট্যাক। প্রোগ্রামিং এর ভাষায় স্ট্যাক এক্সাক্টলি সেইম জিনিসই। তবে একটু ঘষামাজা আছে। আরেকটা উদাহরণ দেয়া যেতে পারে। বয়ামের ভেতর একটার পর আরেকটা বিস্কিট ঢুকিয়ে রাখা হয়। হ্যা, এইটা পারফেক্ট উদাহরণ হয়েছে। এই

ডাটা স্ট্রাকচার- কিউ (Queue)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা কিউ বুঝানোর জন্য। কিউ জিনিসটার সাথে আমরা সবাই পরিচিত। জীবনে আমরা সবাই কখনো না কখনো লাইনে দাঁড়ায়ছি। কিউ এর বেসিক ক্যারেক্টারিস্টিকসের সাথে মিলিয়েই প্রোগ্রামিং এ কিউ এর কনসেপ্ট। বাস্তব জীবনে একটা কিউ তে কি হয়? সবাই লাইন ধরে দাঁড়ায় কিছু একটা কারনে। যে সবার আগে দাঁড়ায় সেই সবার আগে কার্জসিদ্ধি করে। সবার পরের জন সবার পরে। স্ট্যাকের ক্ষেত্রে আমরা পড়েছিলাম Last In First Out (LIFO) or First In Last Out (FILO)। কেমন আনফেয়ার শোনায় না? সবার পরে আসবে, আবার সবার আগে চলে যাবে। অ্যাটলিস্ট আমার আনফেয়ার লেগেছিলো যখন স্ট্যাক শিখছিল

কম্পিউটার বচন

কম্পিউটারের সাথে আমার পরিচয় খুব ছোট বয়সে না। অন্তত চেনাপরিচিত বন্ধুমহলের অনেকের তুলনায় আমি বাচ্চাই বলা চলে। ক্লাস ২ বা ৩ তে পড়ি যখন ফুফাতো ভাই প্রথম কম্পিউটার কেনে। ফুফুর বাসা কাছেই হওয়ায় মাঝে মাঝেই সেটা দেখার সৌভাগ্য হতো। কিন্তু ছুয়ে দেখার সাহস তখনও হয়নি। সেই বছর থেকে আমাদের রেওয়াজ, ঈদের দিন নামাজ পড়েই সোজা ভাইয়ার রুমে। ভাইয়া নতুন একটা গেম ইন্সটল করে রাখতো আর আমরা কাজিনেরা লাইন দিয়ে সেটা খেলতাম (যদিও আমার ভাগে কমই পড়তো :p )  ক্লাস সিক্সে নতুন স্কুলে ভর্তি হলাম। এ এক আজব জায়গা। সবাই বড়লোকের পোলাপাইন। কতজন গাড়িতে করে আসে। তাদের তো কথাই নাই। যাদের গাড়ি নাই তাদেরও রোজকার বাজেট বিশাল। ইচ্ছামত উড়ায়। আর আমি মধ্যবিত্তের ছেলে। যাওয়া আসার ভাড়া বাদে কোনো টাকা পেতামনা। টিফিন বাসা থেকে বানায়ে দিতো। এই যখন আমার অবস্থা তখন ক্লাসমেটরা নিত্যনতুন গ্যাজেটের সাথে শুধু পরিচয়ই না, পকেটে নিয়ে ঘুরে (যদিও স্কুলে নিষিদ্ধ ছিলো )। সবাই এসব নিয়ে কত গল্প চালায় যায়। আর আমার মত বোকা কিছু পাবলিক হা করে এদের গল্প শুনি। কম্পিউটারের কত বচন, কত আড্ডা। এসব আড্ডায় ঢুকতে কত ইচ্ছা করতো, কিন্তু ঢুকবো কি করে? আমার কম্পিউটা