LINQ:寻找一个想法

时间:2011-03-01 23:38:23

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

我从LINQ开始,这是我的代码片段。

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。

3 个答案:

答案 0 :(得分:0)

这是我在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;
        }
    }

}

答案 1 :(得分:0)

我想我有一个可能的答案。 这是我想到的改变。我回答而不是评论的原因是评论中缺乏空间。

我希望我已正确关闭所有大括号。

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;
                            }
                    }

欢迎改进。

答案 2 :(得分:0)

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

来自msdn

  

ToLookup(IEnumerable,   Func)方法返回一个   查找,一对多   将键映射到的字典   价值集合。查找与a不同   字典,哪个   执行密钥的一对一映射   单个值。

     

默认的相等比较器默认值   用于比较键。