📘 C# Blog Series – Blog 14 “C# LINQ — Data को Filter, Sort और Search करने का सबसे आसान तरीका (Real-Life Example: Latest GPS Location निकालना)”

📘 C# Blog Series – Blog 14

“C# LINQ — Data को Filter, Sort और Search करने का सबसे आसान तरीका (Real-Life Example: Latest GPS Location निकालना)”







LINQ (Language Integrated Query)
C# की वो superpower है जिससे आप कोई भी List, Dictionary, Database data को SQL जैसी style में search, filter, sort, group कर सकते हो।

LINQ C# developers का सबसे पसंदीदा feature है क्योंकि:

  • Code छोटा होता है

  • पढ़ने में आसान

  • Powerful filters

  • Zero loops required

  • Real projects में बहुत काम

और आपके GPS Server में तो LINQ लाजवाब काम करता है।

आज हम LINQ को real-life examples + GPS use-case के साथ सीखेंगे।


🟦 1. LINQ Basic Example

बिना LINQ:

List<int> nums = new List<int>() { 1, 2, 3, 4, 5 }; List<int> result = new List<int>(); foreach (var n in nums) { if (n > 2) result.Add(n); }

LINQ के साथ:

var result = nums.Where(n => n > 2).ToList();

One line → Same result
कोई loop नहीं।


🟢 2. Common LINQ Methods (सबसे ज्यादा इस्तेमाल होने वाले)

Where → Filter

OrderBy → Sort

Select → Transform

First, FirstOrDefault → First item

Any → Check exists

Count → Count items

GroupBy → Grouping

Max, Min, Average → Math

इनसे real projects बहुत आसान हो जाते हैं।


🌟 3. Real-Life Example: Latest GPS Location निकालना

आपके पास devices की list है:

List<GPSDevice> devices;

हर device के पास:

  • LastUpdated

  • Latitude

  • Longitude

आपको latest updated device चाहिए:

var latest = devices .OrderByDescending(d => d.LastUpdated) .FirstOrDefault();

Single line → Latest live device मिल गया।


4. Real-Life Example: Offline GPS Devices की List निकालना

var offline = devices .Where(d => d.Status == "Offline") .ToList();

5. Real-Life Example: Low Battery Alerts

var lowBattery = devices .Where(d => d.Battery < 20) .ToList();

अब आप इन devices को SMS/Notification भेज सकते हैं।


6. Real-Life Example: 5 सबसे तेज़ चलने वाले GPS Devices

var topSpeed = devices .OrderByDescending(d => d.Speed) .Take(5) .ToList();

7. Real-Life Example: IMEI से Device Find करना

Without LINQ:

Loop loop loop…

With LINQ:

var d = devices.FirstOrDefault(x => x.IMEI == "861128068064267");

Super clean।


8. Real-Life Example: Daily GPS Logs Grouping by Date

Suppose logs:

List<GPSLog> logs;

Group by date:

var grouped = logs .GroupBy(l => l.Timestamp.Date) .ToList();

आप daily report generate कर सकते हैं।


9. Real-Life Example: Billing Software

Low stock products:

var lowStock = products.Where(p => p.Quantity < 10);

Highest selling products:

var best = products.OrderByDescending(p => p.Sales).Take(10);

10. Real-Life Example: School System

Failed students:

var failed = students.Where(s => s.Marks < 33);

Class topper:

var topper = students.OrderByDescending(s => s.Marks).First();

🔥 11. Real-Life Example: Dictionary में LINQ

If devices stored like:

Dictionary<string, GPSDevice> devicesDict;

Find device:

var d = devicesDict.Values .FirstOrDefault(x => x.IMEI == "861128068064267");

Filter:

var active = devicesDict.Values .Where(d => d.Status == "Active");

🔚 Conclusion

LINQ C# का सबसे powerful feature है:

✔ Filter
✔ Search
✔ Sort
✔ Group
✔ Transform

और वो भी SQL जैसा readable code

GPS servers, e-commerce, billing, school management—
हर जगह LINQ code को clean, fast और maintainable बनाता है।

Post a Comment

0 Comments

Translate

Close Menu