开发者

LINQ: Looking for an idea

开发者 https://www.devze.com 2023-02-14 01:26 出处:网络
I started with LINQ and here\'s a snippet of my code. var filterList = new List<string>() { \"ACRating\",

I started with LINQ and here's a snippet of my code.

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

The issue I am facing is that xList.Count is not a reliable way of looking at list values. Is there some method where I can somehow mark the items in the fi开发者_如何学Clterlist as being a list v/s being a single value. So when I do a comparison in the code, I don't have to rely on xList.Count.


Here's a Sample App I knocked up in LinqPad: M is whatever type your xList is. You could extend it if you need more stuff. I'm sure there are more fluent solutions than this, but I don't have much knowledge of your project this is the best I can do... :)

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

}


I think I have a possible answer. This is the change I have in mind. The reason I am answering instead of commenting is lack of space in comments.

I hope I have closed all the braces correctly.

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

Improvements welcome.


I am still trying to figure out what is going on with the list. However on first glance it appears that you could use .ToLookUp()'

From msdn

The ToLookup(IEnumerable, Func) method returns a Lookup, a one-to-many dictionary that maps keys to collections of values. A Lookup differs from a Dictionary, which performs a one-to-one mapping of keys to single values.

The default equality comparer Default is used to compare keys.

0

精彩评论

暂无评论...
验证码 换一张
取 消