小编Avi*_*atz的帖子

迭代列表并从中删除项目的最佳方法是什么?

我需要迭代List<myObject>并删除回答某个条件的项目.

我看到了这个答案(/sf/answers/110762221/):

使用for循环反向迭代列表:

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));
Run Code Online (Sandbox Code Playgroud)

但我明白,for比效率较低foreach,

所以我想用后面的如下:

foreach (var item in myList.ToList())
{
    // if certain condition applies:
    myList.Remove(item)
}
Run Code Online (Sandbox Code Playgroud)

一种方法比另一种更好吗?

编辑:

我不想使用RemoveAll(...),因为在条件之前循环中有大量代码.

c# iteration list

16
推荐指数
2
解决办法
6887
查看次数

Cypher查询以检查list1是否包含list2中的任何项目

我的数据库中的每个节点都有一个包含列表的属性.我需要检查给定列表中的任何项目是否属于该属性.

我正在寻找一个类似的查询match (n) where any(x in n.list where x=[101,102,103]) return n- 这意味着"检查n.list是否包含101,102,103.如果是,返回n"

在密码中有类似的东西吗?

neo4j cypher

6
推荐指数
1
解决办法
3264
查看次数

覆盖 WPF 中的静态资源

我想覆盖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)

.net wpf resources xaml staticresource

5
推荐指数
0
解决办法
1627
查看次数

有没有办法简单地类似真值表?

有没有简单的方法来代码中的真值表?它有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)

c#

5
推荐指数
1
解决办法
183
查看次数

为什么要检查List <T> .Count <= 0?

我在许多代码片段中看到以下条件用于检查列表是否为空:

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是否定的?

c#

4
推荐指数
1
解决办法
552
查看次数

标签 统计

c# ×3

.net ×1

cypher ×1

iteration ×1

list ×1

neo4j ×1

resources ×1

staticresource ×1

wpf ×1

xaml ×1