NHibernate vs LINQ to SQL

LINQ to SQL forces you to use the table-per-class pattern. The benefits of using this pattern are that it’s quick and easy to implement and it takes very little effort to get your domain running based on an existing database structure. For simple applications, this is perfectly acceptable (and oftentimes even preferable), but for more … Read more

Fun (?) with Linq Expressions in extension methods

If you don’t need the title attribute on individual options your code could be simplified to: public static HtmlString SelectFor<TModel, TProperty, TIdProperty, TDisplayProperty, TListItem>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TListItem> enumeratedItems, Expression<Func<TListItem, TIdProperty>> idProperty, Expression<Func<TListItem, TDisplayProperty>> displayProperty, object htmlAttributes ) where TModel : class { var id = (idProperty.Body as MemberExpression).Member.Name; var display = … Read more

Building Dynamic LINQ Queries based on Combobox Value

Assuming: public class Person { public string LastName { get; set; } } IQueryable<Person> collection; your query: var query = from p in collection where p.LastName == textBox.Text select p; means the same as: var query = collection.Where(p => p.LastName == textBox.Text); which the compiler translates from an extension method to: var query = Queryable.Where(collection, … Read more

How to implement Unit of Work that works with EF and NHibernate

As a side not building solution supporting different provides on top of the linq is way to disaster. Linq and IQueryable are leaky abstractions – each Linq provider can have its own “features” and limitations. Moreover EF itselfs adds some logic via custom extension methods for IQueryable (like Include or AsNoTracking in EFv4.1). These methods … Read more

LINQ Include vs Join. Are they equivalent?

An Included is intended to retain the original object structures and graphs. A Join is needed to project a flattened representation of the object graph or to join types which are not naturally related through the graph (ie. join the customer’s city with a shipping facility’s city). Compare the following: db.Customers.Include(“Orders”) Generates an IEnumerable each … Read more

OrderBy descending in Lambda expression?

As Brannon says, it’s OrderByDescending and ThenByDescending: var query = from person in people orderby person.Name descending, person.Age descending select person.Name; is equivalent to: var query = people.OrderByDescending(person => person.Name) .ThenByDescending(person => person.Age) .Select(person => person.Name);