• <i id='f7fUq'><tr id='f7fUq'><dt id='f7fUq'><q id='f7fUq'><span id='f7fUq'><b id='f7fUq'><form id='f7fUq'><ins id='f7fUq'></ins><ul id='f7fUq'></ul><sub id='f7fUq'></sub></form><legend id='f7fUq'></legend><bdo id='f7fUq'><pre id='f7fUq'><center id='f7fUq'></center></pre></bdo></b><th id='f7fUq'></th></span></q></dt></tr></i><div id='f7fUq'><tfoot id='f7fUq'></tfoot><dl id='f7fUq'><fieldset id='f7fUq'></fieldset></dl></div>
    <legend id='f7fUq'><style id='f7fUq'><dir id='f7fUq'><q id='f7fUq'></q></dir></style></legend>
      <bdo id='f7fUq'></bdo><ul id='f7fUq'></ul>

      <small id='f7fUq'></small><noframes id='f7fUq'>

    1. <tfoot id='f7fUq'></tfoot>

        使用 LINQ 过滤列表

        filtering a list using LINQ(使用 LINQ 过滤列表)
      1. <tfoot id='JkGK5'></tfoot>

          <small id='JkGK5'></small><noframes id='JkGK5'>

          <legend id='JkGK5'><style id='JkGK5'><dir id='JkGK5'><q id='JkGK5'></q></dir></style></legend>
          <i id='JkGK5'><tr id='JkGK5'><dt id='JkGK5'><q id='JkGK5'><span id='JkGK5'><b id='JkGK5'><form id='JkGK5'><ins id='JkGK5'></ins><ul id='JkGK5'></ul><sub id='JkGK5'></sub></form><legend id='JkGK5'></legend><bdo id='JkGK5'><pre id='JkGK5'><center id='JkGK5'></center></pre></bdo></b><th id='JkGK5'></th></span></q></dt></tr></i><div id='JkGK5'><tfoot id='JkGK5'></tfoot><dl id='JkGK5'><fieldset id='JkGK5'></fieldset></dl></div>

              <tbody id='JkGK5'></tbody>
              • <bdo id='JkGK5'></bdo><ul id='JkGK5'></ul>

                1. 本文介绍了使用 LINQ 过滤列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!



                  i have a list of project objects:

                  IEnumerable<Project> projects

                  一个 Project 类作为一个名为 Tags 的属性.这是一个 int[]

                  a Project class as a property called Tags. this is a int[]

                  我有一个名为 filteredTags 的变量,它也是一个 int[].

                  i have a variable called filteredTags which is also a int[].


                  So lets say my filtered tags variable looks like this:

                   int[] filteredTags = new int[]{1, 3};

                  我想过滤我的列表 (projects) 以仅返回具有过滤器中列出的所有标签的项目(在这种情况下,标签中至少有标签 1 和标签 3 属性).

                  I want to filter my list (projects) to only return projects that have ALL of the tags listed in the filter (in this case at least tag 1 AND tag 3 in the Tags property).

                  我试图使用 Where() 和 Contains() 但这似乎只有在我与单个值进行比较时才有效.我将如何将一个列表与另一个列表进行比较,在该列表中我需要匹配过滤列表中的所有项目??

                  I was trying to use Where() and Contains() but that only seems to work if i am comparing against a single value. How would i do this to compare a list against another list where i need a match on all the items in the filtered list ??



                  var filteredProjects = 
                      projects.Where(p => filteredTags.All(tag => p.Tags.Contains(tag)));


                  Honestly, I don't know which one is better, so if performance is not critical, choose the one you think is more readable. If it is, you'll have to benchmark it somehow.

                  可能 Intersect 是要走的路:

                  void Main()
                      var projects = new List<Project>();
                      projects.Add(new Project { Name = "Project1", Tags = new int[] { 2, 5, 3, 1 } });
                      projects.Add(new Project { Name = "Project2", Tags = new int[] { 1, 4, 7 } });
                      projects.Add(new Project { Name = "Project3", Tags = new int[] { 1, 7, 12, 3 } });
                      var filteredTags = new int []{ 1, 3 };
                      var filteredProjects = projects.Where(p => p.Tags.Intersect(filteredTags).Count() == filteredTags.Length);  
                  class Project {
                      public string Name;
                      public int[] Tags;

                  虽然一开始看起来有点难看.如果您不确定它们在列表中是否都是唯一的,您可以先将 Distinct 应用到 filteredTags,否则计数比较将无法按预期进行.

                  Although that seems a little ugly at first. You may first apply Distinct to filteredTags if you aren't sure whether they are all unique in the list, otherwise the counts comparison won't work as expected.

                  这篇关于使用 LINQ 过滤列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  <i id='ApBn7'><tr id='ApBn7'><dt id='ApBn7'><q id='ApBn7'><span id='ApBn7'><b id='ApBn7'><form id='ApBn7'><ins id='ApBn7'></ins><ul id='ApBn7'></ul><sub id='ApBn7'></sub></form><legend id='ApBn7'></legend><bdo id='ApBn7'><pre id='ApBn7'><center id='ApBn7'></center></pre></bdo></b><th id='ApBn7'></th></span></q></dt></tr></i><div id='ApBn7'><tfoot id='ApBn7'></tfoot><dl id='ApBn7'><fieldset id='ApBn7'></fieldset></dl></div>

                      <small id='ApBn7'></small><noframes id='ApBn7'>

                      <tfoot id='ApBn7'></tfoot>
                        • <bdo id='ApBn7'></bdo><ul id='ApBn7'></ul>

                              <tbody id='ApBn7'></tbody>
                            <legend id='ApBn7'><style id='ApBn7'><dir id='ApBn7'><q id='ApBn7'></q></dir></style></legend>