C#: Predicates in LINQ to Entities

If you started programming MVC, in particular the Entity Framework Model, and find yourself filtering your data model using the Where<TSource>() method, you should know by now that you can chain this method instead of using the query syntax.

// using query syntax - calling Where method once
carQuery.Where(c => c.Color == 'red' && c.Price < 10000);

// using method chaining - calling Where method multiple times
carQuery.Where(c => c.Color == 'red').Where(c => c.Price < 10000);

// method chaining is useful in a loop
foreach (KeyValuePair<string, string> filter in filters)
{
string filterVal = filter.Value;
switch (filter.Key)
{
case "Color":
carQuery = carQuery.Where(c => c.Color == filterVal).
break;
case "Price":
int price = Int32.Parse(filterVal);
carQuery = carQuery.Where(c => c.Price < price).
break;
}
}

 

There are other ways to do predicates but  are more advance topic than the ones mentioned above.  The links below talks about them:

ASP.NET: Beginner’s Resource on MVC 4, Entity Framework, and Razor

New to MVC 4/Entity Framework/Razor?  Then here are some links that are useful:

C#: Quick notes on some cool features…

… namely object initializer, collection initializer, implicitly typed local variable, anonymous type, anonymous method, and lambda expression.  

 Object Initializer

// we have here an object we are going to initialize in some other class
public class ToDoItem
{
// the following are the auto-implemented or automatic properties
public int ToDoItemID { get; set; }
public string Description { get; set; }
public DateTime DueDate { get; set; }
public string Notes { get; set; }
}

public class SomeOtherClass
{
//...
private void someMethod()
{
// so below we use the object initializer, enclosed by {}, to set the object's
// properties at creation time without having to define a corresponding
// constructor
// it uses the default constructor to process the the object initializers
ToDoItem toDoItem = new ToDoItem
{
Description = "Attend French class",
DueDate = DateTime.Parse("11/7/2013"),
Notes = "@6:30p"
};
}
}

 

Collection Initializer

// collection initializer lets you specify one or more element initializers
// which can be a simple value, an expression or an object initializer

// initializing a list of ints using simple values
List<int> intList = new List<int> { 1, 2, 3, 4, 5 };

// initializing a list of to do items using object initializers
List<ToDoItem> toDoList = new List<ToDoItem>
{
new ToDoItem { Description = "Visit Milton", ..., Notes = "" },
new ToDoItem { Description = "Attend French class", ..., Notes = "@6:30p" },
new ToDoItem { Description = "Attend Adv JavaScript class", ..., Notes = "@1p" }
};

 

Implicitly Typed Local Variable

// implicitly typed local variable, whose type is determined at compile time, is declared
// using the var keyword
var x = 10;
int y = 10; // explicitly typed

 

Anonymous Type

// anonymous types are objects created with read-only properties and without explicit 
// type
// they are created using the new operator with an object initializer and assigned to a
// variable declared as var
var t = new
{
Description = "Attend French class",
DueDate = DateTime.Parse("11/7/2013"),
Notes = "@6:30p"
};

// they are typically used in the select clause of a LINQ query expression
var toDoQuery =
from toDo in toDoList
select new {toDo.Description, toDo.DueDate};
foreach (var t in toDoQuery)
{
Console.WriteLine(t.Description);
Console.WriteLine(t.DueDate);
}

 

Anonymous Method

// anonymous method is another way of initializing a delegate
// before anonymous method, delegates are initialized with named method that is declared
// elsewhere in the code
// with anonymous method, delegates can now be initialized inline as in below

// first declare the delegate
delegate void IntOpDelegate(int i, int j);

private void DoIntOps()
{
// then instantiate the delegate with unnamed inline statement block called the
// anonymous method
IntOpDelegate intOpSum = delegate(int i, int j)
{
Console.WriteLine("Sum: {0}", i + j);
};

IntOpDelegate intOpDiff = delegate(int i, int j)
{
Console.WriteLine("Diff: {0}", Math.Abs(i - j));
};

// lastly, invoke the delegates
intOpSum(2, 3);
intOpDiff(4, 9);
}

 

Lambda Expression

// lambda expression uses => as the lambda operator
// left side of => contains the input parameter(s) and right side of => contains the
// statement(s) or expression

// example below is using lambda expression in place of anonymous method in our previous
// example
// the right side of => contains one statement
// note that our delegate here does not return a value so the statement should not return
// any value
IntOpDelegate intOpSum2 = (i, j) => Console.WriteLine("Sum: {0}", i + j);
intOpSum2(2, 3);

// a lambda expression example with only 1 input parameter, hence () is not required
IntUnaryOpDelegate intOpInc = i => Console.WriteLine("Increment: {0}", i++);
intOpInc(4);

// a lambda expression example with statements enclosed in {} on the right side of =>
IntUnaryOpDelegate intOpInc2 = i =>
{
Console.Write("Increment: ");
Console.WriteLine(i++);
};
intOpInc2(4);

// here the delegate returns a value so the right side of => contains an expression
// when you say expression it usually returns a value
IntUnaryOpDelegateWithRetVal intOpInc3 = i => i++;
Console.WriteLine("Increment: {0}", intOpInc3(4));

// you can also use statements in place of an expression as in below, but expression is
// more elegant
IntUnaryOpDelegateWithRetVal intOpInc4 = i => { return i++; };
Console.WriteLine("Increment: {0}", intOpInc4(4));

Design Patterns: Strategy, Observer, and Decorator

Here and in the next posts, I will talk about design patterns, it’s definition, the guiding design principles they embody, and their C# examples.  But before you dive into design patterns, here is a refresher on four OO basics.

 

Design Pattern #1: Strategy Pattern

  • Defines a family of algorigthms, encapsulates each one, and makes them interchangeable.  It lets the algorithm vary independently from clients that use it.
  • Design Principles:
    • Design Principle #1 : Separate code that change from code that stays the same
    • Design Principle #2 : Program to an interface and not to an implementation
    • Design Principle #3 : Favor composition (HAS-A) over inheritance (IS-A)
      • Note: To implement composition, a class will have an interface type reference to a concrete implementation that is usually initialized during the class constructor

 

Design Pattern #2: Observer Pattern

  • Defines a one-to-many dependency between objects so that  when one object changes state, all of its dependents are notified and  updated automatically.  A good example is a newspaper publisher and its  subscribers.
  • Design Principles:
    • Design Principle #4 : Strive for loosely coupled designs between objects that interact

 

Design Pattern #3: Decorator Pattern

  • Attaches additional responsibilities to an object dynamically.  Decorators provide a flexible alternative to sub-classing for extending functionality.  It does this by using composition and delegation.  Decorator classes have same types as the components they decorate, either through inheritance or interface implementation.  They add behavior by adding new functionality before and/or after method calls to the component.
  • Design Principles:
    • Design Principle #5 : Classes should be open for extension but closed for modification

Design Patterns: The Four OO Basics

Before going into design patterns, one must need to know the four OO basics:

  • Abstraction is showing only the necessary details to the outside world. It is the abstract form of anything. It is implemented using interface and abstract class.
  • Encapsulation is hiding the details from the outside world. It is the opposite of abstraction. It is implemented using private and protected access modifiers inside a class.
  • Inheritance is a way for an entity to inherit the characteristics of another entity, thus establishing a parent-child relationship, or in a more technical OO term, an IS-A relationship. It is implemented using base and derived classes.