Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从列表中选择/计数元素_C#_Asp.net_List_.net 2.0 - Fatal编程技术网

C# 从列表中选择/计数元素

C# 从列表中选择/计数元素,c#,asp.net,list,.net-2.0,C#,Asp.net,List,.net 2.0,假设我有一个这样的列表 private List<TestClass> test() { List<TestClass> tcList = new List<TestClass>(); tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 3 }); tcList.Add(new TestClass { ModulePosition = 1, Top

假设我有一个这样的列表

private List<TestClass> test()
{
    List<TestClass> tcList = new List<TestClass>();
    tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 3 });
    tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 4, prop3 = 5 });
    tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 6 });

    return tcList;

}
私有列表测试()
{
List tcList=新列表();
Add(新的TestClass{ModulePosition=1,TopBotData=2,prop3=3});
Add(新的TestClass{ModulePosition=1,TopBotData=4,prop3=5});
Add(新测试类{ModulePosition=1,TopBotData=2,prop3=6});
返回tcList;
}

我想做的是,我想返回所有具有
ModulePosition=1
TopBotData=2
的元素。我还需要满足给定条件的计数。在这种情况下,它将是2。不必像我使用.net 2.0那样使用LINQ,您可以将其封装在一个方法中,只需返回符合条件的结果

public IEnumerable<TestClass> GetTests(List<TestClass> tests)
{
   foreach(var v in tests){
      if(v.ModulePosition == 1 && v.TopBotData == 2)
         yield return v;
   }
}
public IEnumerable GetTests(列表测试)
{
foreach(测试中的var v){
if(v.ModulePosition==1&&v.TopBotData==2)
收益率v;
}
}
然后

List<TestClass> tcList = new List<TestClass>();
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 3 });
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 4, prop3 = 5 });
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 6 });

var results = new List<TestClass>(GetTests(tcList));
var count = results.Count;
List tcList=new List();
Add(新的TestClass{ModulePosition=1,TopBotData=2,prop3=3});
Add(新的TestClass{ModulePosition=1,TopBotData=4,prop3=5});
Add(新测试类{ModulePosition=1,TopBotData=2,prop3=6});
var结果=新列表(GetTests(tcList));
var count=results.count;

您可以将其封装在一个方法中,只需返回符合条件的结果即可

public IEnumerable<TestClass> GetTests(List<TestClass> tests)
{
   foreach(var v in tests){
      if(v.ModulePosition == 1 && v.TopBotData == 2)
         yield return v;
   }
}
public IEnumerable GetTests(列表测试)
{
foreach(测试中的var v){
if(v.ModulePosition==1&&v.TopBotData==2)
收益率v;
}
}
然后

List<TestClass> tcList = new List<TestClass>();
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 3 });
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 4, prop3 = 5 });
tcList.Add(new TestClass { ModulePosition = 1, TopBotData = 2, prop3 = 6 });

var results = new List<TestClass>(GetTests(tcList));
var count = results.Count;
List tcList=new List();
Add(新的TestClass{ModulePosition=1,TopBotData=2,prop3=3});
Add(新的TestClass{ModulePosition=1,TopBotData=4,prop3=5});
Add(新测试类{ModulePosition=1,TopBotData=2,prop3=6});
var结果=新列表(GetTests(tcList));
var count=results.count;
公共整数计数(列表测试)
{
int计数器=0;
foreach(测试中的var v){
if(v.ModulePosition==1&&v.topBotData==2)
计数器++;
}
返回计数器;
}
公共整数计数(列表测试)
{
int计数器=0;
foreach(测试中的var v){
if(v.ModulePosition==1&&v.topBotData==2)
计数器++;
}
返回计数器;
}
您可以这样做。 在if中添加您想要的任何条件

        for (int i = 0; i < tcList.Count; i++)
        {
            if (tcList[i].TopBotData == 2 && tcList[i].ModulePosition == 1)
            {
                result.Add(tcList[i]);
            }
        }

        return result;
for(int i=0;i
您可以这样做。 在if中添加您想要的任何条件

        for (int i = 0; i < tcList.Count; i++)
        {
            if (tcList[i].TopBotData == 2 && tcList[i].ModulePosition == 1)
            {
                result.Add(tcList[i]);
            }
        }

        return result;
for(int i=0;i
您可以这样做。 在if中添加您想要的任何条件

为了知道元素的数量,只需执行
result.Count

        for (int i = 0; i < tcList.Count; i++)
        {
            if (tcList[i].TopBotData == 2 && tcList[i].ModulePosition == 1)
            {
                result.Add(tcList[i]);
            }
        }

        return result;
for(int i=0;i
您可以这样做。 在if中添加您想要的任何条件

为了知道元素的数量,只需执行
result.Count

        for (int i = 0; i < tcList.Count; i++)
        {
            if (tcList[i].TopBotData == 2 && tcList[i].ModulePosition == 1)
            {
                result.Add(tcList[i]);
            }
        }

        return result;
for(int i=0;i
我同意Eoin的答案,但我会使用更通用的方法,例如

private List<TestClass> GetByModuleAndTopBot(List<TestClass> list, int modulePosition, int topBotData)
{
    List<TestClass> result = new List<TestClass>();
    foreach (TestClass test in list)
    {
        if ((test.ModulePosition == modulePosition) &&
            (test.TopBotData == topBotData))
            result.Add(test);
    }
    return result;
}

然后用
.count
对它进行计数,因为它的返回类型是一个
列表
我同意Eoin的回答,但我会使用一种更通用的方法,例如

private List<TestClass> GetByModuleAndTopBot(List<TestClass> list, int modulePosition, int topBotData)
{
    List<TestClass> result = new List<TestClass>();
    foreach (TestClass test in list)
    {
        if ((test.ModulePosition == modulePosition) &&
            (test.TopBotData == topBotData))
            result.Add(test);
    }
    return result;
}
并使用
.count
对其进行计数,因为其返回类型是
列表

具有与LINQ的
几乎相同的值,其中

return tcList.FindAll(
  delegate(TestClass x) { return x.ModulePosition == 1 && x.topBotData == 2; });
在较新版本的.NET中,我建议使用LINQ和lambda表达式,但对于.NET 2.0来说,上述方法可能是实现所需操作的最简洁的方法(因此,IMHO可能是一种好方法)。

return tcList.FindAll(
  delegate(TestClass x) { return x.ModulePosition == 1 && x.topBotData == 2; });

在较新版本的.NET中,我建议使用LINQ和lambda表达式,但对于.NET 2.0,上述方法可能是实现所需操作的最简洁的方法(因此,IMHO可能是一种好方法)。

您还可以使用谓词:

private static bool MyFilter(TestClass item)
{
  return (item.ModulePosition) == 1 && (item.TopBotData == 2);
}
private static void Example()
{
  List<TestClass> exampleList = test();
  List<TestClass> sortedList = exampleList.FindAll(MyFilter);
}
private静态bool MyFilter(TestClass项)
{
返回(item.ModulePosition)==1&(item.TopBotData==2);
}
私有静态void示例()
{
List exampleList=test();
List sortedList=exampleList.FindAll(MyFilter);
}

您还可以使用谓词:

private static bool MyFilter(TestClass item)
{
  return (item.ModulePosition) == 1 && (item.TopBotData == 2);
}
private static void Example()
{
  List<TestClass> exampleList = test();
  List<TestClass> sortedList = exampleList.FindAll(MyFilter);
}
private静态bool MyFilter(TestClass项)
{
返回(item.ModulePosition)==1&(item.TopBotData==2);
}
私有静态void示例()
{
List exampleList=test();
List sortedList=exampleList.FindAll(MyFilter);
}

到目前为止,您尝试了什么?您在实施此问题的解决方案时遇到了哪些问题?您当前的尝试中是否有一些不起作用的代码?到目前为止您尝试了什么?您在实施此问题的解决方案时遇到了哪些问题?您当前的尝试中是否有一些不工作的代码?修复了使用列表(IEnumerable)构造函数修复了使用列表(IEnumerable)构造函数