My Software Notes

Useful things I discover

Archive for the ‘LINQ’ Category

Entity Framework: Contains() (aka ‘Like’) is case insensitive for SQL Server queries

with one comment

Just a note to self.

The Contains method on String when used in an Entity Framework “where” clause is case insensitive.


Assume the “NoteContent” column on the table “Notes” is a varchar column. If I run the following Linq query against the database where db is a DbContext then I get the same result every time.

var num = db.Notes.Where(n => n.NoteContent.Contains(“NoTe”)).Count();

var num = db.Notes.Where(n => n.NoteContent.Contains(“NOTE”)).Count();

var num = db.Notes.Where(n => n.NoteContent.Contains(“note”)).Count();

All produce the same value for “num”.

I’m making this note to self because I keep forgetting and because I keep seeing people do this:

var someString = “whatever”;

var num = db.Notes.Where(n => n.NoteContent.ToUpper().Contains(someString.ToUpper())).Count();

And, to quote Led Zepplin, “It makes me wonder.”

Written by gsdwriter

December 2, 2014 at 9:53 am

Posted in .NET, Database, LINQ

Adding that “all” element to a combo box

leave a comment »

In the past when data binding a combo box, I’ve found that adding the “(all)” or “(none)” element to the top of the list has always seems unaesthetic to me.  A funny choice of words, you may think, but to me well written code is aesthetic and adding that top element just seemed messy. 

If you were using a data set then you had to create a row and stick it in the table in the data set and then sort the thing so your element would come to the top and then bind to the table or to the data view.

If you weren’t data binding but were loading elements in a loop, you still had to stick that “all” element in there to make sure it got on top.

Binding to a collection of objects was a bit cleaner but I still didn’t much care for it.

But now we have LINQ and at last I’ve found the aesthetic way to do it!

using (var db = new ProductDataContext())
    var results = from p in db.ProductTypes
                  orderby p.Description
                  select p;
//Add an "All" item to the top of the list
var all = new ProductType()
    ProductTypeCode = PRODUCT_TYPE_CODE_ALL,
    Description = PRODUCT_TYPE_DESCR_ALL
prodTypeCollection.Insert(0, all);

I like LINQ.  It is making my job a hell-uva-lot easier and more enjoyable.

Written by gsdwriter

June 7, 2009 at 8:56 pm

Posted in Languages, LINQ

Tagged with , ,