Using System.Data.Linq in a Razor view

You need to import the namespace into your view by adding @using System.Data.Linq at the top of your view. However if you want it in all your views then you need to add <add namespace=”System.Data.Linq” /> to the web.config in your Views folder: <system.web.webPages.razor> <host factoryType=”System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ /> <pages pageBaseType=”System.Web.Mvc.WebViewPage”> <namespaces> <add … Read more

Linq-to-sql orderby thenby

How can I do an orderby thenby using the above syntax without using extension syntax. Use a comma between the fields: orderby a, b And what does the orderby, orderby do? When you use orderby twice in a row the elements conceptually will first be sorted using the first orderby, and then sorted again using … Read more

LINQ Take(); how to handle when null or fewer records than requested available?

There’s a difference between a null reference and an empty collection. It’s fine to call Take on an empty collection. And the argument specifies a maximum number to take, so it’s also fine to specify more than there are items in the collection. I recommend referring to MSDN for precise details like this. For Linq … Read more

LINQ return items in a List that matches any Names (string) in another list

var products = shopProducts.Where(p => listOfProducts.Any(l => p.Name == l.Name)) .ToList(); For LINQ-to-Objects, if listOfProducts contains many items then you might get better performance if you create a HashSet<T> containing all the required names and then use that in your query. HashSet<T> has O(1) lookup performance compared to O(n) for an arbitrary IEnumerable<T>. var names … Read more

Automatically Compile Linq Queries

You can’t have extension methods invoked on anonymous lambda expressions, so you’ll want to use a Cache class. In order to properly cache a query you’ll also need to ‘lift’ any parameters (including your DataContext) into parameters for your lambda expression. This results in very verbose usage like: var results = QueryCache.Cache((MyModelDataContext db) => from … Read more

LINQ across multiple databases

You can do this, even across servers, as long as you can access one database from the other. That is, if it’s possible to write a SQL statement against ServerA.DatabaseA that accesses ServerB.DatabaseB.schema.TableWhatever, then you can do the same thing in LINQ. To do it, you’ll need to edit the .dbml file by hand. You … Read more

How to dynamically add OR operator to WHERE clause in LINQ

You can use the PredicateBuilder class: var searchPredicate = PredicateBuilder.False<Songs>(); foreach(string str in strArray) { var closureVariable = str; // See the link below for the reason searchPredicate = searchPredicate.Or(SongsVar => SongsVar.Tags.Contains(closureVariable)); } var allSongMatches = db.Songs.Where(searchPredicate); LinqToSql strange behaviour

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