我需要迭代List<myObject>并删除回答某个条件的项目.
我看到了这个答案(/sf/answers/110762221/):
使用for循环反向迭代列表:
Run Code Online (Sandbox Code Playgroud)for (int i = safePendingList.Count - 1; i >= 0; i--) { // some code // safePendingList.RemoveAt(i); }例:
Run Code Online (Sandbox Code Playgroud)var list = new List<int>(Enumerable.Range(1, 10)); for (int i = list.Count - 1; i >= 0; i--) { if (list[i] > 5) list.RemoveAt(i); } list.ForEach(i => Console.WriteLine(i));
但我明白,for比效率较低foreach,
所以我想用后面的如下:
foreach (var item in myList.ToList())
{
// if certain condition applies:
myList.Remove(item)
}
Run Code Online (Sandbox Code Playgroud)
一种方法比另一种更好吗?
编辑:
我不想使用RemoveAll(...),因为在条件之前循环中有大量代码.
我的数据库中的每个节点都有一个包含列表的属性.我需要检查给定列表中的任何项目是否属于该属性.
我正在寻找一个类似的查询match (n) where any(x in n.list where x=[101,102,103]) return n- 这意味着"检查n.list是否包含101,102,103.如果是,返回n"
在密码中有类似的东西吗?
我想覆盖StaticResource在我自己的资源字典中的不同程序集的资源字典中配置的 a 。我尝试使用相同的密钥配置新资源,但没有成功。实际加载的资源来自所提到的程序集的资源字典。
出于演示目的,我将资源称为“MyResource”:
MyResourceDictionary.xaml:
<ResourceDictionary xmlns=...>
<!-- I use the same key as the original resource, from the other assembly -->
<DataTemplate x:Key="MyResource">
<!--My own implementation of that resource -->
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
应用程序.xaml
<Application x:Class=...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Resource dictionary from different assembly -->
<ResourceDictionary Source="pack://application:,,,/Assembly;component/ResourceDictionary.xaml"/>
<!-- My resource dictionary -->
<ResourceDictionary Source="pack://application:,,,/MyApplication;component/ResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud) 有没有简单的方法来代码中的真值表?它有2个输入和4个结果,如下所示:
我目前的代码是:
private void myMethod(bool param1, bool param2)
{
Func<int, int, bool> myFunc;
if (param1)
{
if (param2)
myFunc = (x, y) => x >= y;
else
myFunc = (x, y) => x <= y;
}
else
{
if (param2)
myFunc = (x, y) => x < y;
else
myFunc = (x, y) => x > y;
}
//do more stuff
}
Run Code Online (Sandbox Code Playgroud) 我在许多代码片段中看到以下条件用于检查列表是否为空:
List<string> someList = someFunctionThatPopulatesAList();
if (someList == null || someList.Count <= 0)
return;
Run Code Online (Sandbox Code Playgroud)
我想知道 - 为什么不使用以下条件:
if (someList == null || someList.Count == 0)
return;
Run Code Online (Sandbox Code Playgroud)
有什么情况List<T>.Count是否定的?