Ordering nullable DateTime in Linq to SQL

This is a bit of a hack, but it appears to work with Linq to SQL: return from ju in context.Job_Users_Assigned where ju.UserID == user.ID orderby ju.Created ?? DateTime.MaxValue descending; So I’m substituting the maximum possible DateTime value when the actual “Create” value is null. That’ll put all the null values at the top. Another … Read more

Cleanest Way To Map Entity To DTO With Linq Select?

Just use AutoMapper. Example: Mapper.CreateMap<Address, AddressDTO>(); Mapper.CreateMap<Person, PersonDTO>(); Your query will execute when the mapping is performed but if there are fields in the entity that you’re not interested use Project().To<> which is available both for NHibernate and EntityFramework. It will effectively do a select on the fields specified in the mapping configurations.

Creating a common predicate function

The neat thing about how LINQ to SQL handles expressions is that you can actually build out expressions elsewhere in your code and reference them in your queries. Why don’t you try something like this: public static class Predicates { public static Expression<Func<User, bool>> CheckForFlags() { return (user => user.Flag1 || user.Flag2 || user.Flag3 || … Read more

DISTINCT() and ORDERBY issue

From the Queryable.Distinct documentation; The expected behavior is that it returns an unordered sequence of the unique items in source. In other words, any order the existing IQueryable has is lost when you use Distinct() on it. What you want is probably something more like this, an OrderBy() after the Distinct() is done; var query … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)