{"id":2832,"date":"2026-04-09T14:56:00","date_gmt":"2026-04-09T06:56:00","guid":{"rendered":"https:\/\/www.xfan.top\/?p=2832"},"modified":"2026-04-09T14:56:00","modified_gmt":"2026-04-09T06:56:00","slug":"c-net-framework-4-8-%e4%b8%8b%e4%bd%bf%e7%94%a8entity-framework-6%e7%94%9f%e6%88%90sql-like%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.xfan.top\/index.php\/2026\/04\/09\/2832\/","title":{"rendered":"C# .NET Framework 4.8 \u4e0b\u4f7f\u7528Entity Framework 6\u751f\u6210SQL LIKE\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p>EF6\u6ca1\u6709\u5185\u7f6e\u7684Like\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u81ea\u5b9a\u4e49\u65b9\u6cd5\u4ee3\u66ff\u751f\u6210<\/p>\n<pre><code>\/\/\/ &lt;summary&gt;\n\/\/\/ \u5e94\u7528\u5168\u5c40\u5173\u952e\u5b57\u641c\u7d22\uff08\u591a\u5b57\u6bb5OR\u8fde\u63a5\u7684\u6a21\u7cca\u641c\u7d22\uff09\n\/\/\/ &lt;\/summary&gt;\nprivate IQueryable&lt;TEntity&gt; ApplyGlobalKeywordSearch(IQueryable&lt;TEntity&gt; query, string keyword, List&lt;string&gt; propertyNames)\n{\n    if (string.IsNullOrEmpty(keyword) || propertyNames == null || !propertyNames.Any())\n        return query;\n\n    ParameterExpression parameter = Expression.Parameter(typeof(TEntity), &quot;x&quot;);\n    Expression combinedExpression = null;\n\n    \/\/ \u901a\u914d\u7b26\u5305\u88f9\n    string likePattern = &quot;%&quot; + keyword + &quot;%&quot;;\n    ConstantExpression likePatternExpression = Expression.Constant(likePattern, typeof(string));\n\n    foreach (string propertyName in propertyNames)\n    {\n        PropertyInfo property = typeof(TEntity).GetProperty(propertyName,\n            BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);\n\n        if (property == null || property.PropertyType != typeof(string))\n            continue;\n\n        MemberExpression propertyAccess = Expression.Property(parameter, property);\n\n        \/\/ EntityFunctions.Like(x.Property, &quot;%keyword%&quot;)\n        MethodInfo likeMethod = typeof(DbFunctions).GetMethod(&quot;Like&quot;, new[] { typeof(string), typeof(string) });\n\n        var likeExpression = Expression.Call(\n            likeMethod,\n            propertyAccess,\n            likePatternExpression\n        );\n\n        if (combinedExpression == null)\n        {\n            combinedExpression = likeExpression;\n        }\n        else\n        {\n            combinedExpression = Expression.OrElse(combinedExpression, likeExpression);\n        }\n    }\n\n    if (combinedExpression == null)\n        return query;\n\n    var lambda = Expression.Lambda&lt;Func&lt;TEntity, bool&gt;&gt;(combinedExpression, parameter);\n    return query.Where(lambda);\n}\n<\/code><\/pre>\n<p>\u53ef\u751f\u6210SQL\u8bed\u53e5\u4e2d\u7684 Like % keyword %<\/p>\n","protected":false},"excerpt":{"rendered":"<p>EF6\u6ca1\u6709\u5185\u7f6e\u7684Like\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u81ea\u5b9a\u4e49\u65b9\u6cd5\u4ee3\u66ff\u751f\u6210 \/\/\/ &lt;summary&gt; \/\/\/  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2832","post","type-post","status-publish","format-standard","hentry","category-webappdev"],"_links":{"self":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/comments?post=2832"}],"version-history":[{"count":5,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2832\/revisions"}],"predecessor-version":[{"id":2849,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2832\/revisions\/2849"}],"wp:attachment":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/media?parent=2832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/categories?post=2832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/tags?post=2832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}