代码之家  ›  专栏  ›  技术社区  ›  ExternalUse

LINQ列表包含(x= >…)-CS0103x不存在于当前上下文中(在前文内)

c#
  •  2
  • ExternalUse  · 技术社区  · 6 年前

    我有两个简单的列表要同步。 List<Guid> untrackedList List<Foo> trackedList 包含许多对象 Foo 具有Guid的ID属性的。此列表由实体框架跟踪,因此我要为未跟踪列表中的未跟踪列表中的未跟踪列表添加新项,并从未跟踪列表中移除未跟踪列表中的所有项。

    public void MyTestMethod()
    {
        const int someThing = 1000;
        var untrackedList = new List<Guid>()
        {
            new Guid("8fcfb512-ca00-4463-b98a-ac890b3ac4da"),
            new Guid("6532b60b-f047-4a96-9e5f-c5a242f9a1f5"),
            new Guid("103cb7e4-1674-490c-b299-4b20d90e706c"),
            new Guid("6c933cce-fb0e-4e1b-bbc3-e62235933cc8")
        };
        var trackedList = new List<Foo>()
        {
            new Foo() { SomeId = new Guid("6532b60b-f047-4a96-9e5f-c5a242f9a1f5"), Something = someThing },
            new Foo() { SomeId = new Guid("12345678-abcd-1234-1234-1234567890ab"), Something = someThing }
        };
        // testing Find and Exists
        var testFind = trackedList.Find(x => x.SomeId == new Guid("6532b60b-f047-4a96-9e5f-c5a242f9a1f5")); // finds one Foo
        var testExists = trackedList.Exists(x => x.SomeId == new Guid("12345678-abcd-1234-1234-1234567890ab")); // == true
    
        foreach (var guid in untrackedList)
        {
            // add all items not yet in tracked List
            if (!trackedList.Exists(x => x.SomeId == guid))
            {
                trackedList.Add(new Foo() { SomeId = guid, Something = someThing });
            }
        }
        // now remove all from trackedList that are not also in untracked List (should remove 12345678-...)
        trackedList.RemoveAll(x=> !untrackedList.Contains(x.SomeId)); // successful, but also shows CS0103 in the debugger
    }
    

    Debugger Screenshot

    是什么导致了这个错误?为什么没有例外?

    0 回复  |  直到 6 年前