English 中文(简体)
LINQ含有 %和 escape
原标题:LINQ contains appends % and escapes %
  • 时间:2011-02-02 15:04:13
  •  标签:
  • c#
  • .net
  • linq

当Im使用LINQ来制造有活力的 s时,我不希望它预先确定和计算成百分数,如果在我身上使用百分数的Im不希望逃脱。 它避开了我所增加的百分比标志,将~用作逃生顺序之前的预留标记。

例如:

string str =  %test%.doc%  
.Contains(str) // converts this into LIKE  %~%test~%.doc~%% 

预期转化:%

最佳回答
问题回答

集装箱可能会被转化为在Kall使用LIKE的操作器。 该操作员将<>%作为野心。 <代码>Contains ("abc”)map to LIKE %abc%

我利用以下延长时间来避免这种情况(尽管在我的具体情况下,我仍然使用野心,但你可以自行修改。

public static bool Like(this string value, string term)
    {
        Regex regex = new Regex(string.Format("^{0}$", term.Replace("*", ".*")), RegexOptions.IgnoreCase);
        return regex.IsMatch(value ?? string.Empty);
    }

    public static IEnumerable<string> Like(this IEnumerable<string> source, string expression)
    {
        return (from s in source where s.Like(expression) select s);
    }

不幸的是,我不认为这样做是容易的,但这可能是行之有效的:

var a = from t in Db.Tests
        let i1 = t.Name.IndexOf("test")
        let i2 = t.Name.IndexOf(".doc")
        where i1 != -1 && i2 != -1 && i1 < i2
        select t;

方法链中的等同:

Db.Tests.Select(t => new {t, i1 = t.Name.IndexOf("test")}).Select(
                @t1 => new {@t1, i2 = @t1.t.Name.IndexOf(".doc")}).Where(
                    @t1 => @t1.@t1.i1 != -1 && @t1.i2 != -1 && @t1.@t1.i1 < @t1.i2).Select(@t1 => @t1.@t1.t);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签