Dot Net TechnologyUnit 55 min read
LINQ & Querying Data: Syntax, Operators, Real-World Apps
Unit 5 of Dot Net Technology covers LINQ (Language Integrated Query) in C, including query syntax, standard operators (Select, Where, OrderBy), method syntax, and real-world database querying with ADO.NET. Learn how to filter, sort, and transform data collections like a pro.
What is LINQ?
LINQ (Language Integrated Query) is a query syntax built into C# that allows you to query data sources (collections, databases, XML, etc.) using SQL-like syntax or method calls. It integrates seamlessly with C# and works with:
- In-memory collections (arrays, lists, dictionaries)
- Databases (via ADO.NET)
- XML/JSON data
Why Use LINQ?
- Cleaner code: Replaces manual loops with declarative queries.
- Type safety: Compile-time checks for errors.
- Flexibility: Works with any data source that implements
IEnumerable<T>.
LINQ Syntax Types
LINQ supports two syntaxes:
- Query Syntax (SQL-like)
- Method Syntax (Lambda expressions)
1. Query Syntax Example
var employees = from emp in Employees
where emp.Salary > 20000
where emp.Address == "Kathmandu"
select emp;
Visualization: Query Execution Flow
flowchart TD
A["Employees Collection"] --> B["Filter: Salary > 20000"]
B --> C["Filter: Address = 'Kathmandu'"]
C --> D["Select: Return matching employees"]2. Method Syntax Example
var employees = Employees
.Where(emp => emp.Salary > 20000)
.Where(emp => emp.Address == "Kathmandu")
.Select(emp => emp);
Comparison Table: Query vs. Method Syntax
| Feature | Query Syntax | Method Syntax |
|---|---|---|
| Readability | SQL-like, easier for SQL devs | Lambda-based, concise |
| Flexibility | Less flexible | More flexible (e.g., Aggregate) |
| Performance | Slightly slower | Optimized by compiler |
Standard LINQ Operators
1. Where (Filtering)
Filters elements based on a condition. Example:
var highEarners = Employees.Where(e => e.Salary > 20000);
Visualization: Filtering in Action
graph LR
A["All Employees"] -->|"Salary ≤ 20000"| B["Discarded"]
A -->|"Salary > 20000"| C["Selected"]2. Select (Projection)
Transforms data into a new form. Example:
var names = Employees.Select(e => e.Name);
3. OrderBy / OrderByDescending (Sorting)
Sorts data in ascending/descending order. Example:
var sortedEmployees = Employees.OrderBy(e => e.Salary);
4. GroupBy (Grouping)
Groups data by a key. Example:
var departments = Employees.GroupBy(e => e.Department);
LINQ with Databases (ADO.NET)
LINQ works with ADO.NET to query databases. Example: Fetch Employees from SQL Server
using (var context = new EmployeeDBEntities())
{
var query = from emp in context.Employees
where emp.Salary > 20000
select emp;
foreach (var emp in query) Console.WriteLine(emp.Name);
}
Visualization: ADO.NET + LINQ Flow
sequenceDiagram
C# Code->>ADO.NET: Query via LINQ
ADO.NET->>SQL Server: Executes SQL
SQL Server-->>ADO.NET: Returns Data
ADO.NET-->>C# Code: LINQ ResultReal-World Applications of LINQ
1. eSewa (Nepal) – Transaction Filtering
- Use Case: eSewa uses LINQ to filter transactions by date, amount, and user.
- Example Query:
var recentTransactions = Transactions .Where(t => t.Date > DateTime.Now.AddDays(-7)) .OrderByDescending(t => t.Amount);
2. Daraz (Nepal) – Order Processing
- Use Case: Daraz uses LINQ to sort orders by priority (e.g., "urgent" vs. "standard").
- Example Query:
var urgentOrders = Orders .Where(o => o.Priority == "Urgent") .OrderBy(o => o.DeliveryTime);
3. Bank Loan Approval (Nepalese Banks)
- Use Case: Banks use LINQ to filter loan applicants based on credit score and income.
- Example Query:
var eligibleApplicants = Applicants .Where(a => a.CreditScore > 600) .Where(a => a.Income > 50000);
Worked Example: Employee Management System
Problem: Find employees in Kathmandu earning >20,000. Solution (Query Syntax):
var result = from emp in Employees
where emp.Salary > 20000 && emp.Address == "Kathmandu"
select new { emp.Name, emp.Salary };
Output:
| Name | Salary |
|---|---|
| Ram | 25000 |
| Sita | 30000 |
Visualization: Step-by-Step Filtering
graph TD
A["All Employees"] --> B["Salary > 20000"]
B --> C["Address = 'Kathmandu'"]
C --> D["Selected Employees"]Exam Tip
- Know both syntaxes (Query vs. Method).
- Practice common operators (
Where,Select,OrderBy). - Combine LINQ with ADO.NET for database queries.
- Use real-world examples (eSewa, Daraz, banks) in answers.
- Avoid memory leaks when querying large datasets (use
ToList()orToArray()).
Key Takeaways:
- LINQ integrates queries into C# for cleaner code.
- Query Syntax is SQL-like; Method Syntax uses lambdas.
- Standard operators (
Where,Select,OrderBy) are exam staples. - ADO.NET + LINQ enables database querying.
- Real-world apps (eSewa, Daraz) use LINQ for filtering/sorting.
Based on the TU BCA syllabus for Dot Net Technology (CACS408), unit 5.
Discussion
Loading…