Like operator or using wildcards in LINQ to Entities

前端 未结 6 1404
抹茶落季
抹茶落季 2021-02-06 05:12

I\'m using LINQ 2 Entities. Following is the problem:

string str = \'%test%.doc%\' 
.Contains(str) // converts this into LIKE \'%~%test~%.doc~%%\'
6条回答
  •  耶瑟儿~
    2021-02-06 05:27

    Use a regular expression...

    The following will print out all of the files in the current directory that match test.doc* (dos wildcard style - which I believe is what you're asking for)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    
    namespace RegexFileTester
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] _files = Directory.GetFiles(".");
                var _fileMatches =  from i in _files
                                    where Regex.IsMatch(i, ".*test*.doc.*")
                                    //where Regex.IsMatch(i, ".*cs")
                                    select i;
                foreach(var _file in _fileMatches)
                {
                    Console.WriteLine(_file);
                }
            }
        }
    }
    

提交回复
热议问题