【问题标题】:LINQ: Looking for an ideaLINQ:寻找一个想法
【发布时间】:2011-03-01 23:38:23
【问题描述】:

我从 LINQ 开始,这是我的代码的 sn-p。

var filterList = new List<string>()
            {
                "ACRating",
                "Axles",
                "SafetyCodes",
                "BuiltInFeatures"
            }

foreach( var i in filterList )
{
  var filter = i;
  var xList = Vehicles.FilterSpecList( filter );

  foreach ( var j in xList )
  {
    if ( xList.Count() == 1 ) /*Will Not Work since a list could have a single value.*/
    {
      switch( j.FeatureName )
      {                 
        case "ACRating":
          v.AcRating = j.Value;
          Console.WriteLine( j );
          break;
       }
    }
    else
    {
      switch( j.FeatureName )
      {
        //Am trying to still figure out how to get all the items in BuiltInFeatures, but you get the idea.
        case "BuiltInFeatures"
        {
          v.BuiltInFeatures = "MP3" + "SUNROOF";
          break;
        }
      }
    }
  }
}

我面临的问题是 xList.Count 不是查看列表值的可靠方法。是否有某种方法可以让我以某种方式将过滤器列表中的项目标记为列表 v/s 是单个值。所以当我在代码中做比较时,我不必依赖xList.Count。

【问题讨论】:

  • 我想知道是否更容易构建字典而不是列表来定义过滤器。
  • 我不确定你想用这段代码去哪里?什么是 xList?为什么需要“Count() == 1”检查?
  • @DaveShaw。 VehicleFilter 类返回两种类型的项目。这些项目在一个列表中。重复的项目是一个列表。单个项目是属性。因此,例如,列表将包含一个或多个标记为“BuiltInFeatures”的项目。这将作为列表处理。如果列表包含单个项目,它将被分配给一个属性。
  • 啊,我明白了 :) 让我想想 2 分钟
  • Vehicles.FilterSpecList() 的返回类型到底是什么?在您的大多数示例中使用 var 应该显式声明,因为没有我们普通人可以识别的容易识别的隐式类型。

标签: c# .net linq .net-3.5


【解决方案1】:

这是我在 LinqPad 中创建的示例应用程序: M 是您的 xList 的任何类型。如果你需要更多的东西,你可以扩展它。 我敢肯定有比这更流畅的解决方案,但我对你的项目了解不多,这是我能做的最好的...... :)

class M
{
    public String FeatureName;
    public IEnumerable<String> Value;
}

M FilterSpecList(String filterName)
{
    if (filterName == "ACRating")
        return new M {FeatureName = "ACRating", Value = new [] {"OK",}};
    else if (filterName == "BuiltInFeatures")
        return new M {FeatureName = "BuiltInFeatures", Value = new[] {"MP3", "Sunroof",}};
    else
        return new M();//throw new Exception("More..");
}

void Main()
{
    List<String> filterList = new List<String>()
    {
        "ACRating",
        "Axles",
        "SafetyCodes",
        "BuiltInFeatures",
    };

    foreach (String filter in filterList)
    {
        var xList = FilterSpecList(filter);

        switch (xList.FeatureName)
        {
            case "ACRating":
                Console.WriteLine(xList.Value.Single());
                break;
            case "BuiltInFeatures":
                Console.WriteLine(String.Join(" + ", xList.Value));
                break;
            default:
                break;
        }
    }

}

【讨论】:

    【解决方案2】:

    我想我有一个可能的答案。 这是我心目中的改变。我回答而不是评论的原因是 cmets 空间不足。

    我希望我已经正确地关闭了所有的大括号。

    var filterList = new Dictionary<string, string>
                                   {
                                        {"ACRating", "Property"},
                                        {"Axles", "Property"}
                                        {"SafetyCodes","List"}
                                        {"BuiltInFeatures","List"}
                                   };   
    
                    foreach(KeyValuePair<string,string> i in filterList)
                    {
                        var filter = i.Key;
                        var xList = Vehicles.FilterSpecList(filter);
                        if (i.Value == "List")
                        {
                            foreach (var j in xList)
                            {
                                switch(j.FeatureName)
                                {
                                case "BuiltInFeatures"
                                {
                                        v.BuiltInFeatures = "x," + "y";
                                        break;
                                }
                        }
                        else if(i.Value == "Property")
                        {
                    foreach (var j in xList)
                            {
                            switch(j.FeatureName)
                                {                   
                                    case "ACRating":
                                        v.AcRating = j.Value;
                                        Console.WriteLine(j.ToString());
                                        break;
                                }
                        }
    

    欢迎改进。

    【讨论】:

      【解决方案3】:

      我仍在试图弄清楚该列表发生了什么。但是乍一看,您似乎可以使用.ToLookUp()'

      来自msdn

      ToLookup(IEnumerable, Func) 方法返回一个 查找,一对多 将键映射到的字典 值的集合。查找不同于 字典,其中 执行键的一对一映射 到单个值。

      默认相等比较器 Default 用于比较键。

      【讨论】:

        猜你喜欢
        • 2011-02-19
        • 2021-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-18
        相关资源
        最近更新 更多