corigin.com

sofware news

LINQ Question / Contains Extension Method / Substring comparison

Posted in Live (November 26, 2007 at 12:58 am)



I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I’m doing quite some increasing LINQ evangelism down here in Germany…).

 

Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine:

 

List<string> searchTerms = new List<string>() { "Maria", "Pedro" };

                       

var query = from c in db.Customers

            where searchTerms.Contains(c.ContactName)

            select c;

 

            dataGridView1.DataSource = query;

 

 

The problem with this code is, that c.ContactName has to match exactly “Maria” or “Pedro”. It does not include substring search, so given a ContactName might be “Maria Foo” or “Pedro Bar” it does not return those rows. Any idea as to how to achieve this without iterating through query in a foreach loop?

 

Sidenote:

Visual Studio 2008 IntelliSense indicates that there is a Contains<> overload which accepts an IEqualityComparer type. 

 

However, the following code compiles fine but throws a runtime exception:

 

SubstringComparer substringComparer = new SubstringComparer();

 

List<string> searchTerms = new List<string>() { "Maria", "Pedro" };

                       

var query = from c in db.Customers

            where searchTerms.Contains<string>(c.ContactName, substringComparer)

            select c;

 

Any idea how to declare a constraint which returns rows which contain keywords contained within a collection? 

 

…more

MSRC Blog: Security Advisory (944653)Windows Live MessengerMS07-069 Cumulative Security Update for Internet Explorer - Post Install IssueMSRC Blog: Security Advisory 943521New Detection Logic for May 8th Office 2007 Updates

No Responses to “LINQ Question / Contains Extension Method / Substring comparison”

  1. Bevis Says:

    go to take a look at Direct gag-hardcore.buyonlinesite.com/ creates the need for .

Leave a Reply

You must be logged in to post a comment.