R
Ralf Rottmann \(www.24100.net\)
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?
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?