From: Mr. Arnold on
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.
>

And the workaround is to use the Contains function in the Linq query
against the row and column in the row.
From: Arne Vajhøj on
On 27-05-2010 11:33, 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.

If available memory and performance allows it then just load
everything and use regex.

See example below.

Arne

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq.SqlClient;
using System.Text.RegularExpressions;

namespace E
{
[Table(Name="T1")]
public class T1
{
[Column(Name="F1",IsPrimaryKey=true)]
public int f1;
[Column(Name="F2")]
public string f2;
}

public class Program
{
public static void Main(string[] args)
{
using(SqlConnection con = new
SqlConnection(@"Server=ARNEPC3\SQLEXPRESS2008;Integrated
Security=SSPI;Database=Test"))
{
DataContext db = new DataContext(con);
Table<T1> t1 = db.GetTable<T1>();
Console.WriteLine("Contains:");
IEnumerable<T1> a = t1.Where(r => r.f2.Contains("B"));
foreach(T1 r in a)
{
Console.WriteLine(r.f1 + " " + r.f2);
}
Console.WriteLine("Regex:");
IEnumerable<T1> b = t1.ToList().Where(r =>
Regex.IsMatch(r.f2, "^.*B.*$"));
foreach(T1 r in b)
{
Console.WriteLine(r.f1 + " " + r.f2);
}
Console.ReadKey();
}
}
}
}