zoukankan      html  css  js  c++  java
  • C#将QueryString参数列表,拼接成Where条件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace ConsoleGetQueryStringDemo
    {
        /// <summary>
        ///     C#将QueryString参数列表,拼接成Where条件
        ///     LDH @ 2021-11-17
        /// </summary>
        internal class Program
        {
            private static void Main()
            {
                Console.Title = "C#将QueryString参数列表,拼接成Where条件";
    
                Method();
    
                Method2();
    
                Console.ReadKey();
            }
    
            /// <summary>
            ///     https://stackoverflow.com/questions/14652402/convert-query-string-array-parameters-into-dictionary-value
            ///     Convert query string array parameters into dictionary value
            ///     LDH @ 2021-11-17
            /// </summary>
            private static void Method()
            {
                var input = "?where[column]=value&where[column2]=value2&orderby[column]=asc&orderby[column2]=desc";
                Console.WriteLine($"原始QueryString:{Environment.NewLine}{input}");
                PrintLine();
    
                var pattern = @"(?<Type>[^[]+)\[(?<Key>[^]]+)\]=(?<Value>.+)";
    
                var re = new Regex(pattern);
    
                var dict = input.Split(new[] {"?", "&"}, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => re.Match(s))
                    .GroupBy(m => m.Groups["Type"].Value)
                    .ToDictionary(g => g.Key,
                        g => g.ToDictionary(x => x.Groups["Key"].Value,
                            x => x.Groups["Value"].Value));
    
                foreach (var t in dict.Keys)
                {
                    Console.WriteLine("Type: " + t);
                    foreach (var k in dict[t].Keys) Console.WriteLine("{0}: {1}", k, dict[t][k]);
                }
    
                /*
                    Type: where
                    column: value
                    column2: value2
                    Type: orderby
                    column: asc
                    column2: desc
                 */
    
                PrintLine();
            }
    
            /// <summary>
            ///     QueryString:Customer=DOSILICON&CustPoName=7&CustPoNo=&CustPoVer=1
            ///     拼接为 Where条件
            ///     Customer='DOSILICON' AND CustPoName='7' AND CustPoVer='1'
            /// </summary>
            private static void Method2()
            {
                var input = "Customer=DOSILICON&CustPoName=7&CustPoNo=&CustPoVer=1";
                Console.WriteLine($"原始QueryString:{Environment.NewLine}{input}");
                PrintLine();
    
                var dic = input.Split(new[] {"&"}, StringSplitOptions.RemoveEmptyEntries);
    
                var list = new List<string>();
    
                foreach (var item in dic)
                {
                    // 去除无用的参数(值为空的)
                    if (item.EndsWith("=")) continue;
    
                    /*
                        Customer=DOSILICON
                        CustPoName=7
                        CustPoVer=1
                     */
                    Console.WriteLine(item);
    
                    list.Add(item);
                }
    
                PrintLine();
    
                var whereStr = "SELECT * FROM LDH WHERE 1 = 1 AND ";
    
                foreach (var item in list)
                {
                    var index = item.IndexOf('=');
                    var sth = item.Substring(0, index) + " = " + "'" + item.Substring(index + 1, item.Length - index - 1) +
                              "'";
                    Console.WriteLine(sth);
    
                    whereStr += sth + "";
                }
    
                PrintLine();
    
                if (whereStr.EndsWith(""))
                {
                    whereStr = whereStr.Trim().TrimEnd('').Replace("", "AND");
                    Console.WriteLine($"最终拼接的SQL:{Environment.NewLine}{whereStr}");
                }
    
                PrintLine();
            }
    
            private static void PrintLine()
            {
                Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
            }
        }
    }

     

    本文作者:Love In Winter
    本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/15567827.html
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
    扫一扫,支付宝打赏 扫一扫,微信打赏
  • 相关阅读:
    红黑树
    二叉搜索树
    散列表
    快速排序
    堆排序
    归并排序
    插入排序
    Shell脚本之:函数
    Shell脚本之:退出循环
    ACM刷题之路(四)2018暑假实验室集训——深广搜专题题解
  • 原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/15567827.html
Copyright © 2011-2022 走看看