【问题标题】:create querystring using lambda from List T使用 List T 中的 lambda 创建查询字符串
【发布时间】:2015-01-11 08:36:10
【问题描述】:

我在创建正确的选择时遇到了一些困难。

我有我的自定义类:

internal class classA{
        internal string FieldName { get; set; }
        internal string FieldValue { get; set; }
        internal bool IncludeInChecksum { get; set; }
}

我要做的是使用上面的类列表构建和连接查询字符串。 对于以下对象:

List<classA> requestParams = ...

查询字符串应如下所示: a=1&b=2&c=3

按字段名升序排列

其中 IncludeInChecksum = true

字段名 = 字段值

            preMD5= requestParams
                .Where(x=>x.IncludeInChecksum)
                .OrderBy(y=>y.FieldName)
                .Aggregate((i,j)=>(i.FieldName+ "=" +j.FieldValue));

这就是我卡住的地方。 提前致谢

【问题讨论】:

  • 您对Aggregate 工作原理的理解不正确。您可能只想要一个 Select 最后。
  • 那么你的代码的结果是什么?
  • 没有结果..我不知道如何从这里继续

标签: c# linq list lambda


【解决方案1】:

我将尝试借助 Select() 方法获取所有 name=value 字符串。然后将结果转换为数组。之后只需使用String.Join() 方法即可获得所需的结果。

Join(String, String[]) 连接字符串数组的所有元素,在每个元素之间使用指定的分隔符。

var preMD5= requestParams
                    .Where(x => x.IncludeInChecksum)
                    .OrderBy(y => y.FieldName)
                    .Select(z => string.Format("{0}={1}", z.FieldName, z.FieldValue))
                    .ToArray();
preMD5 = string.Join("&", preMD5);

【讨论】:

    【解决方案2】:

    Aggregate 聚合来自不同行的值。您需要组合来自不同字段的值。为此,您使用Select:

    requestParms.Where(...).OrderBy(...).Select(f=>f.FieldName+"="+f.FieldValue)
    

    这将返回name=value 字符串的IEnumerable。您可以使用string.Join 将它们组合成一个字符串。

    【讨论】:

      【解决方案3】:

      我知道已经回答了, 但我仍然想提供我的解决方案。

      我使用了专门的方法来构建查询字符串参数, 和一个扩展方法来连接它。

      希望这会有所帮助。

      public class classA
      {
          internal string FieldName { get; set; }
          internal string FieldValue { get; set; }
          internal bool IncludeInChecksum { get; set; }
      
          public classA(string fieldName, string fieldValue, bool includeInChecksum)
          {
              this.FieldName = fieldName;
              this.FieldValue = fieldValue;
              this.IncludeInChecksum = includeInChecksum;
          }
      
          public string ToQueryString()
          {
              return string.Format("{0}={1}",
                  this.FieldName,
                  this.FieldValue);
          }
      
          public void Test()
          {
              var list = new List<classA> { 
                  new classA("A", "1", true) ,
                  new classA("D", "4", true) ,
                  new classA("B", "2", false)  ,
                  new classA("C", "3", true) 
              };
      
              var result = list.
                  Where(o => o.IncludeInChecksum).
                  OrderBy(o => o.FieldName).
                  Select(o => o.ToQueryString()).
                  ToStringEx("&");
          }
      
      }
      
      public static class ExtensionMethods
      {
          public static string ToStringEx<T>(this IEnumerable<T> items, string separetor = ",")
          {
              if (items == null)
              {
                  return "null";
              }
      
              return string.Join(separetor, items.Select(o => o != null ? o.ToString() : "[null]").ToArray());
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-28
        • 1970-01-01
        • 1970-01-01
        • 2017-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多