Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在LINQ中使用星号(*)搜索_C#_Linq - Fatal编程技术网

C# 如何在LINQ中使用星号(*)搜索

C# 如何在LINQ中使用星号(*)搜索,c#,linq,C#,Linq,我想用*搜索。 我有 我希望有人能理解我。10倍您必须自己编写一些代码 示例查询: var q = (from c in db.Customers where c.CompanyName.Contains(name) select c) .ToList(); 上面的示例将始终在CompanyName中的任何位置搜索 火柴但是你需要给你的用户更多的控制权 通过允许它们在任意位置提供通配符来匹配方法 要匹配的文本的开头或结尾。这就意味着你只能 根

我想用*搜索。 我有


我希望有人能理解我。10倍

您必须自己编写一些代码

示例查询:

var q = (from c in db.Customers
         where c.CompanyName.Contains(name)
         select c)
        .ToList();
上面的示例将始终在CompanyName中的任何位置搜索 火柴但是你需要给你的用户更多的控制权 通过允许它们在任意位置提供通配符来匹配方法 要匹配的文本的开头或结尾。这就意味着你只能 根据查询的存在和位置动态生成查询 通配符


找到了这个问题的根源

请分享实际的C代码。现在还不清楚你在做什么。你想从列表中得到结果吗?请不要只发布链接答案!添加一些更多的信息,这样它可能会生存链接腐烂。我希望你已经要求博客作者复制和粘贴他的作品在这里。。。无论如何,是的,这是更好的,我已经添加了源代码,我使用了引号,我并没有声称这是我的工作,所以我认为它没有问题。
I set in textbox: Michael - 1 result, 
I set in textbox: Michael* - 20 results
var q = (from c in db.Customers
         where c.CompanyName.Contains(name)
         select c)
        .ToList();
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

    public static class LinqExtensions
    {
        public static IQueryable<TSource> WhereLike<TSource>(
            this IQueryable<TSource> source,
            Expression<Func<TSource, string>> valueSelector,
            string value,
            char wildcard)
        {
            return source.Where(BuildLikeExpression(valueSelector, value, wildcard));
        }

        public static Expression<Func<TElement, bool>> BuildLikeExpression<TElement>(
            Expression<Func<TElement, string>> valueSelector,
            string value,
            char wildcard)
        {
            if (valueSelector == null)
                throw new ArgumentNullException("valueSelector");

            var method = GetLikeMethod(value, wildcard);

            value = value.Trim(wildcard);
            var body = Expression.Call(valueSelector.Body, method, Expression.Constant(value));

            var parameter = valueSelector.Parameters.Single();
            return Expression.Lambda<Func<TElement, bool>>(body, parameter);
        }

        private static MethodInfo GetLikeMethod(string value, char wildcard)
        {
            var methodName = "Contains";

            var textLength = value.Length;
            value = value.TrimEnd(wildcard);
            if (textLength > value.Length)
            {
                methodName = "StartsWith";
                textLength = value.Length;
            }

            value = value.TrimStart(wildcard);
            if (textLength > value.Length)
            {
                methodName = (methodName == "StartsWith") ? "Contains" : "EndsWith";
                textLength = value.Length;
            }

            var stringType = typeof(string);
            return stringType.GetMethod(methodName, new Type[] { stringType });
        }
    }
var searchTerm = "*Inc";
var q = db.Customers
        .WhereLike(c => c.CompanyName, searchTerm, '*')
        .ToList();