LINQ:寻找想法

发布于 2024-10-19 23:45:36 字数 1056 浏览 3 评论 0 原文

我从 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 不是查看列表值的可靠方法。有没有某种方法可以让我以某种方式将过滤器列表中的项目标记为列表而不是单个值。所以当我在代码中进行比较时,我不必依赖xList.Count。

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 filterlist 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十雾 2024-10-26 23:45:36

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

}

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

}
亢潮 2024-10-26 23:45:36

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

我希望我已经正确地闭合了所有牙套。

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

欢迎改进。

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.

樱桃奶球 2024-10-26 23:45:36

我仍在试图弄清楚列表中发生了什么。然而乍一看,您似乎可以使用

.ToLookUp()' “nofollow”>msdn

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文