
View Message Thread (12 replies)
| how to use 'Like' in this linq query? |
 |
From: Rich P Date Posted: 5/26/2010 12:33:00 PM
--pseudo code here with the Like keyword -- how to apply it?
var query1 = from DataRow myRow in ds1.Tables["tblA"].Rows
where (string)myRow["Company"] Like "test"
select myRow;
I found this sample on the net:
list = list.Where(o => SqlMethods.Like(o.FirstName, FirstName))
How could I apply something like this to the query above?
Rich
*** Sent via Developersdex http://www.developersdex.com ***

| Re: how to use 'Like' in this linq query? |
|
From: Rich P Date Posted: 5/26/2010 12:39:00 PM
I forgot to include the wildcards:
var query1 = from DataRow myRow in ds1.Tables["tblA"].Rows
where (string)myRow["Company"] Like "%test%"
select myRow;
how to handle 'Like' and wildcards with linq?
note: this query works if it is a straight '=='
Thanks
Rich
*** Sent via Developersdex http://www.developersdex.com ***

| Re: how to use 'Like' in this linq query? |
|
From: Rich P Date Posted: 5/26/2010 1:01:00 PM
I added a reference to System.Data.Linq
and a
using System.Data.Linq.SqlClient;
and attempted the following query:
var query1 = from DataRow myRow in ds1.Tables["tblA"].Rows
where SqlMethods.Like(myRow["Company"], "%test%")
select myRow;
In the error message that ensued -- it mentioned that SqlMethods
currently only works with Linq To Sql. I guess this is Linq to a
Datatable. The rest of the error said that I had some invalid argument
--
perhaps myRow["Company"] ?
Rich
*** Sent via Developersdex http://www.developersdex.com ***

| Re: how to use 'Like' in this linq query? |
|
From: Alberto Poblacion Date Posted: 5/26/2010 1:03:00 PM
"Rich P" < rpng123@aol.com> wrote in message
news:%23py3aJQ$KHA.5044@TK2MSFTNGP04.phx.gbl...
> var query1 = from DataRow myRow in ds1.Tables["tblA"].Rows
> where (string)myRow["Company"] Like "%test%"
> select myRow;
You can express the preceding with the "Contains" method:
var query1 = from myRow in ds1.Tables["tblA"].AsEnumerable()
where myrow.field<string>("Company").Contains("test")
select myRow:
If you only want the "%" at the end, you can use "StartsWith" instead of
"Contains".

| Re: how to use 'Like' in this linq query? |
|
From: Rich P Date Posted: 5/26/2010 1:30:00 PM
Thanks. I will give that a try. Although, in my searching, other
articles suggested that Contains does not handle wildcards. I guess I
will find out.
Thanks again for the reply and suggestion.
Rich
*** Sent via Developersdex http://www.developersdex.com ***

| Re: how to use 'Like' in this linq query? |
|
From: Mr. Arnold Date Posted: 5/26/2010 2:50:00 PM
Rich P wrote:
> I added a reference to System.Data.Linq
>
> and a
>
> using System.Data.Linq.SqlClient;
>
> and attempted the following query:
>
> var query1 = from DataRow myRow in ds1.Tables["tblA"].Rows
> where SqlMethods.Like(myRow["Company"], "%test%")
> select myRow;
>
>
> In the error message that ensued -- it mentioned that SqlMethods
> currently only works with Linq To Sql. I guess this is Linq to a
> Datatable. The rest of the error said that I had some invalid argument
It's going to be this I believe.
myRow.Contains("something")

| Re: how to use 'Like' in this linq query? |
|
From: Arne Vajhøj Date Posted: 5/26/2010 8:10:00 PM
On 26-05-2010 15:26, Rich P wrote:
> Thanks. I will give that a try. Although, in my searching, other
> articles suggested that Contains does not handle wildcards. I guess I
> will find out.
LIKE '%foobar%' is .Contains("foobar"), but for more
advanced usages of LIKE you will need to do something
like a selecting where a lambda using regex returns
true.
Arne

| Re: how to use 'Like' in this linq query? |
|
From: Rich P Date Posted: 5/27/2010 9:35:00 AM
I attempted something like this:
list = list.Where(o => SqlMethods.Like(o.FirstName, FirstName))
but the error message said that SqlMethods.Like only works with Linq to
Sql. I am trying to use Linq on a dataTable in my C# (2008) winform
app. Any thoughts appreciated if there is a workaround for using
SqlMethods against a dataTable contained in the app.
Rich
*** Sent via Developersdex http://www.developersdex.com ***

| Re: how to use 'Like' in this linq query? |
|
From: Patrice Date Posted: 5/27/2010 12:33:00 PM
Keep in mind that Linq is not SQL but just C#. So Contains be translated to
%Value%, StartsWith to Value% and EndsWith to %Value.
If you need more specific patterns server side, you'll have to create your
own function mapping.
http://msdn.microsoft.com/en-us/library/dd456828(v=VS.100).aspx should help.
--
Patrice
"Rich P" < rpng123@aol.com> a écrit dans le message de groupe de discussion :
Ol5wYlQ$KHA.5280@TK2MSFTNGP05.phx.gbl...
> Thanks. I will give that a try. Although, in my searching, other
> articles suggested that Contains does not handle wildcards. I guess I
> will find out.
>
> Thanks again for the reply and suggestion.
>
> Rich
>
> *** Sent via Developersdex http://www.developersdex.com ***
>

| Re: how to use 'Like' in this linq query? |
|
From: Mr. Arnold Date Posted: 5/27/2010 4:59:00 PM
Rich P wrote:
> I attempted something like this:
>
> list = list.Where(o => SqlMethods.Like(o.FirstName, FirstName))
>
>
> but the error message said that SqlMethods.Like only works with Linq to
> Sql. I am trying to use Linq on a dataTable in my C# (2008) winform
> app. Any thoughts appreciated if there is a workaround for using
> SqlMethods against a dataTable contained in the app.
>
You can't use the statement as it only pertains to querying a SQL Server
table on the entity model using Linq.
http://msdn.microsoft.com/en-us/library/system.data.linq.sqlclient.sqlmethods.like.aspx

Would you like to track this thread?
By adding this News Thread to your Favorites Area you can refer to it later with just a click of the mouse.
Also you can optionally be notified instantly whenever there are any replies
posted to this Thread. To add this Thread to your Favorites Area just click on the red arrow next to the subject of the thread above
.
|