Connecting Tech Pros Worldwide Forums | Help | Site Map

LINQ Question (Contains)

BeSharp
Guest
 
Posts: n/a
#1: Nov 25 '07
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<stringsearchTerms = 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<stringsearchTerms = 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?


--

-------------------------------
http://www.24100.net

--

-------------------------------
http://www.24100.net


Steven Cheng[MSFT]
Guest
 
Posts: n/a
#2: Nov 26 '07

re: LINQ Question (Contains)


Hi BeSharp,

As for the D LINQ selection scenario you mentioned, I think it sounds like
what you want to do is a SQL "LIKE" comparison. For {"...",
"..."}.contains, "contains" actually means that the input parameter exactly
match any of the i tem in collection, that is not substring mapping. For
substring mapping, you should use SQL "Like" style selection. Here is a web
thread discussing on using "LIKE" query in LINQ:

#using LIKE in DLinq
http://forums.microsoft.com/MSDN/Sho...08989&SiteID=1

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.





--------------------
Quote:
>From: "BeSharp" <BeSharp@community.nospam>
>Subject: LINQ Question (Contains)
>Date: Sun, 25 Nov 2007 13:28:55 +0100
>
>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<stringsearchTerms = 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<stringsearchTerms = 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?
>
>
>--
>
>-------------------------------
>http://www.24100.net
>
>--
>
>-------------------------------
>http://www.24100.net
>
>
Steven Cheng[MSFT]
Guest
 
Posts: n/a
#3: Nov 28 '07

re: LINQ Question (Contains)


Hi BeSharp,

Does the info in my last reply helps some or have you got any further
progress?


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Quote:
>From: stcheng@online.microsoft.com (Steven Cheng[MSFT])
>Organization: Microsoft
>Date: Mon, 26 Nov 2007 06:17:43 GMT
>Subject: RE: LINQ Question (Contains)
>
>Hi BeSharp,
>
>As for the D LINQ selection scenario you mentioned, I think it sounds like
>what you want to do is a SQL "LIKE" comparison. For {"...",
>"..."}.contains, "contains" actually means that the input parameter
exactly
Quote:
>match any of the i tem in collection, that is not substring mapping. For
>substring mapping, you should use SQL "Like" style selection. Here is a
web
Quote:
>thread discussing on using "LIKE" query in LINQ:
>
>#using LIKE in DLinq
>http://forums.microsoft.com/MSDN/Sho...08989&SiteID=1
>
>Hope this helps.
>
>Sincerely,
>
>Steven Cheng
>
>Microsoft MSDN Online Support Lead
>
>
>
>================================================= =
>
>Get notification to my posts through email? Please refer to
>http://msdn.microsoft.com/subscripti...ault.aspx#noti
f
Quote:
>ications.
>
>
>
>Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
>where an initial response from the community or a Microsoft Support
>Engineer within 1 business day is acceptable. Please note that each follow
>up response may take approximately 2 business days as the support
>professional working with you may need further investigation to reach the
>most efficient resolution. The offering is not appropriate for situations
>that require urgent, real-time or phone-based interactions or complex
>project analysis and dump analysis issues. Issues of this nature are best
>handled working with a dedicated Microsoft Support Engineer by contacting
>Microsoft Customer Support Services (CSS) at
>http://msdn.microsoft.com/subscripti...t/default.aspx.
>
>================================================= =
>
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
>
>
>--------------------
Quote:
>>From: "BeSharp" <BeSharp@community.nospam>
>>Subject: LINQ Question (Contains)
>>Date: Sun, 25 Nov 2007 13:28:55 +0100
>>
>>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<stringsearchTerms = 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
Quote:
Quote:
>>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<stringsearchTerms = 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?
>>
>>
>>--
>>
>>-------------------------------
>>http://www.24100.net
>>
>>--
>>
>>-------------------------------
>>http://www.24100.net
>>
>>
>
>
Ralf Rottmann \(www.24100.net\)
Guest
 
Posts: n/a
#4: Nov 28 '07

re: LINQ Question (Contains)


Steven,

Sorry for not responding... I blogged about my solution here:

http://www.talentgrouplabs.com/blog/...se-part-2.aspx



"Steven Cheng[MSFT]" <stcheng@online.microsoft.comwrote in message
news:hxVybAbMIHA.7908@TK2MSFTNGHUB02.phx.gbl...
Quote:
Hi BeSharp,
>
Does the info in my last reply helps some or have you got any further
progress?
>
>
Sincerely,
>
Steven Cheng
>
Microsoft MSDN Online Support Lead
>
>
This posting is provided "AS IS" with no warranties, and confers no
rights.
>
--------------------
Quote:
>>From: stcheng@online.microsoft.com (Steven Cheng[MSFT])
>>Organization: Microsoft
>>Date: Mon, 26 Nov 2007 06:17:43 GMT
>>Subject: RE: LINQ Question (Contains)
>>
>>Hi BeSharp,
>>
>>As for the D LINQ selection scenario you mentioned, I think it sounds like
>>what you want to do is a SQL "LIKE" comparison. For {"...",
>>"..."}.contains, "contains" actually means that the input parameter
exactly
Quote:
>>match any of the i tem in collection, that is not substring mapping. For
>>substring mapping, you should use SQL "Like" style selection. Here is a
web
Quote:
>>thread discussing on using "LIKE" query in LINQ:
>>
>>#using LIKE in DLinq
>>http://forums.microsoft.com/MSDN/Sho...08989&SiteID=1
>>
>>Hope this helps.
>>
>>Sincerely,
>>
>>Steven Cheng
>>
>>Microsoft MSDN Online Support Lead
>>
>>
>>
>>================================================ ==
>>
>>Get notification to my posts through email? Please refer to
>>http://msdn.microsoft.com/subscripti...ault.aspx#noti
f
Quote:
>>ications.
>>
>>
>>
>>Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
>>where an initial response from the community or a Microsoft Support
>>Engineer within 1 business day is acceptable. Please note that each follow
>>up response may take approximately 2 business days as the support
>>professional working with you may need further investigation to reach the
>>most efficient resolution. The offering is not appropriate for situations
>>that require urgent, real-time or phone-based interactions or complex
>>project analysis and dump analysis issues. Issues of this nature are best
>>handled working with a dedicated Microsoft Support Engineer by contacting
>>Microsoft Customer Support Services (CSS) at
>>http://msdn.microsoft.com/subscripti...t/default.aspx.
>>
>>================================================ ==
>>
>>
>>This posting is provided "AS IS" with no warranties, and confers no
>>rights.
>>
>>
>>
>>
>>
>>--------------------
Quote:
>>>From: "BeSharp" <BeSharp@community.nospam>
>>>Subject: LINQ Question (Contains)
>>>Date: Sun, 25 Nov 2007 13:28:55 +0100
>>>
>>>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<stringsearchTerms = 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
Quote:
Quote:
>>>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<stringsearchTerms = 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?
>>>
>>>
>>>--
>>>
>>>-------------------------------
>>>http://www.24100.net
>>>
>>>--
>>>
>>>-------------------------------
>>>http://www.24100.net
>>>
>>>
>>
>>
>
Steven Cheng[MSFT]
Guest
 
Posts: n/a
#5: Nov 29 '07

re: LINQ Question (Contains)


Thanks for your followup and sharing the info with us.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Quote:
>From: "Ralf Rottmann \(www.24100.net\)" <BeSharp@live.com>
>References: <1B0B01A3-23D5-460B-9196-A109A1F75ADA@microsoft.com>
<2#V8VQ$LIHA.7908@TK2MSFTNGHUB02.phx.gbl>
<hxVybAbMIHA.7908@TK2MSFTNGHUB02.phx.gbl>
Quote:
>Subject: Re: LINQ Question (Contains)
>Date: Wed, 28 Nov 2007 21:29:35 +0100
>
>Steven,
>
>Sorry for not responding... I blogged about my solution here:
>
>http://www.talentgrouplabs.com/blog/...c-linq-queries
--dynamic-where-clause-part-2.aspx
Quote:
>
>
>
>"Steven Cheng[MSFT]" <stcheng@online.microsoft.comwrote in message
>news:hxVybAbMIHA.7908@TK2MSFTNGHUB02.phx.gbl...
Quote:
>Hi BeSharp,
>>
>Does the info in my last reply helps some or have you got any further
>progress?
>>
>>
>Sincerely,
>>
>Steven Cheng
>>
>Microsoft MSDN Online Support Lead
>>
>>
>This posting is provided "AS IS" with no warranties, and confers no
>rights.
>>
>--------------------
Quote:
>>>From: stcheng@online.microsoft.com (Steven Cheng[MSFT])
>>>Organization: Microsoft
>>>Date: Mon, 26 Nov 2007 06:17:43 GMT
>>>Subject: RE: LINQ Question (Contains)
>>>
>>>Hi BeSharp,
>>>
>>>As for the D LINQ selection scenario you mentioned, I think it sounds
like
Quote:
Quote:
Quote:
>>>what you want to do is a SQL "LIKE" comparison. For {"...",
>>>"..."}.contains, "contains" actually means that the input parameter
>exactly
Quote:
>>>match any of the i tem in collection, that is not substring mapping. For
>>>substring mapping, you should use SQL "Like" style selection. Here is a
>web
Quote:
>>>thread discussing on using "LIKE" query in LINQ:
>>>
>>>#using LIKE in DLinq
>>>http://forums.microsoft.com/MSDN/Sho...08989&SiteID=1
>>>
>>>Hope this helps.
>>>
>>>Sincerely,
>>>
>>>Steven Cheng
>>>
>>>Microsoft MSDN Online Support Lead
>>>
>>>
>>>
>>>=============================================== ===
>>>
>>>Get notification to my posts through email? Please refer to
>>>http://msdn.microsoft.com/subscripti...efault.aspx#no
ti
Quote:
Quote:
>f
Quote:
>>>ications.
>>>
>>>
>>>
>>>Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
Quote:
Quote:
Quote:
>>>where an initial response from the community or a Microsoft Support
>>>Engineer within 1 business day is acceptable. Please note that each
follow
Quote:
Quote:
Quote:
>>>up response may take approximately 2 business days as the support
>>>professional working with you may need further investigation to reach the
>>>most efficient resolution. The offering is not appropriate for situations
>>>that require urgent, real-time or phone-based interactions or complex
>>>project analysis and dump analysis issues. Issues of this nature are best
>>>handled working with a dedicated Microsoft Support Engineer by contacting
>>>Microsoft Customer Support Services (CSS) at
>>>http://msdn.microsoft.com/subscripti...t/default.aspx.
>>>
>>>=============================================== ===
>>>
>>>
>>>This posting is provided "AS IS" with no warranties, and confers no
>>>rights.
>>>
>>>
>>>
>>>
>>>
>>>--------------------
>>>>From: "BeSharp" <BeSharp@community.nospam>
>>>>Subject: LINQ Question (Contains)
>>>>Date: Sun, 25 Nov 2007 13:28:55 +0100
>>>>
>>>>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
Quote:
Quote:
Quote:
>>>>column contains keywords from a list of keywords. The following works
>>>>just
>>>>fine:
>>>>
>>> List<stringsearchTerms = 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
Quote:
>>>>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:
Quote:
Quote:
Quote:
>>>>
>>> SubstringComparer substringComparer = new
>>>SubstringComparer();
>>>>
>>> List<stringsearchTerms = new List<string>() { "Maria",
>>>>"Pedro" };
>>>>
>>> var query = from c in db.Customers
>>> where
searchTerms.Contains<string>(c.ContactName,
Quote:
Quote:
Quote:
>>>>substringComparer)
>>> select c;
>>>>
>>>>Any idea how to declare a constraint which returns rows which contain
>>>>keywords contained within a collection?
>>>>
>>>>
>>>>--
>>>>
>>>>-------------------------------
>>>>http://www.24100.net
>>>>
>>>>--
>>>>
>>>>-------------------------------
>>>>http://www.24100.net
>>>>
>>>>
>>>
>>>
>>
>
Closed Thread


Similar C# / C Sharp bytes