LINQ to SQL and Null strings, how do I use Contains?
I’d use the null-coalescing operator… (from a in this._addresses where (a.Street ?? “”).Contains(street) || (a.StreetAdditional ?? “”).Contains(streetAdditional) select a).ToList<Address>()
I’d use the null-coalescing operator… (from a in this._addresses where (a.Street ?? “”).Contains(street) || (a.StreetAdditional ?? “”).Contains(streetAdditional) select a).ToList<Address>()
var user = this.dataContext.Users.FirstOrDefault( p => p.User_ID == 250 && p.UserName == “Jack”); The p => at the beginning counts for the whole expression. The syntax used here is a shorthand for (p) => { return p.User_ID == 250 && p.UserName == “Jack”; }
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
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.
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
DbContext has method for this: var set = context.Set<MyEntity>();
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
Try the following code: return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();
This is more like an extension to @Alex’s answer. Step 1 : Set the connection property of your .dbml file to “none”. Step 2 : Create a new separate partial class with the same name as that of the existing partial class for the .dbml file. And set the connectionString property by using the parameterless … Read more
AFAIK, this isn’t supported by the object model. However, LINQ supports a method to execute a query (strangly enough called DataContext.ExecuteQuery). Looks like you can use that to call a arbitrary piece of SQL and map it back to LINQ. You won’t be SQL free because of the embedded SQL, but you won’t have to … Read more