我有两个对象,一个AnotherList包含一个整数数组,另一个MyFolder包含一个对象FolderItem数组,该对象ItemKeyword包含一个具有两个字符串(在键值中)的对象List<FolderItem>的数组对样式)。

我想从MyFolder返回在AnotherList中未引用的Contains。我可以获得列表中的项目列表,并在底部包括了Linq。

我整天都在与。.ExceptFolderItem扩展名作斗争,但始终遇到错误。我希望这对某人来说很容易。

在这种情况下,代码说出一千个单词,事实就是如此。

最后的Linq查询在folderItemID=25时刻仅返回一个FolderItems

我需要它返回所有folderItemID=26,27,28

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;

namespace temp
{
    class MyFolder
    {
        public FolderItem[] items { get; set; }
    }

    class FolderItem
    {
        public int folderItemID { get; set; }
        public ItemKeyword[] keywords { get; set; }
    }

    class ItemKeyword
    {
        public string key { get; set; }
        public string value { get; set; }
    }

    class AnotherList
    {
        public AnotherListItem[] items { get; set; }
    }

    class AnotherListItem
    {
        public int dataID { get; set; }
    }




    public class TestingClass
    {

        public static void mainApp()
        {
            AnotherList List1 = new AnotherList()
                {
                    items = new AnotherListItem[]{
                        new AnotherListItem(){dataID=1},
                        new AnotherListItem(){dataID=2},
                        new AnotherListItem(){dataID=3}
                    }};

            MyFolder List2 = new MyFolder()
            {
                items = new FolderItem[]
                {
                    new FolderItem()
                        {
                            folderItemID=25, keywords= new ItemKeyword[]
                                {
                                    new ItemKeyword(){key="dataID", value="1"},
                                    new ItemKeyword(){key="description", value="some text"},
                                }
                    },
                    new FolderItem()
                        {
                            folderItemID=26, keywords= new ItemKeyword[]
                                {
                                    new ItemKeyword(){key="dataID", value="4"},
                                    new ItemKeyword(){key="description", value="some other text"},
                                }
                        },
                    new FolderItem()
                        {
                            folderItemID=27, keywords= new ItemKeyword[]
                                {
                                    new ItemKeyword(){key="dataID", value="9"},
                                    new ItemKeyword(){key="description", value="even more other text"},
                                }
                        },
                    new FolderItem()
                        {
                            folderItemID=28, keywords= new ItemKeyword[]
                                {
                                    new ItemKeyword(){key="dataID", value="12"},
                                    new ItemKeyword(){key="description", value="3"},
                                }
                        }
                }};

            List<FolderItem> res = (from someItems in List2.items
                                    from itemKeywords in someItems.keywords
                                    join otherItems in List1.items on itemKeywords.value equals otherItems.dataID.ToString()
                                    where itemKeywords.key == "dataID"
                                    select someItems).ToList<FolderItem>();

        }
    }

}

最佳答案

尝试使用Any扩展方法。您可以放心地复制并粘贴以下代码,因为它可以与您提供的代码一起使用...

        List<FolderItem> res = List2.items.Where(x => !List1.items.Any(y => x.keywords.FirstOrDefault(z => z.key == "dataID").value == y.dataID.ToString())).ToList();

关于c# - 无法执行缺席,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20707594/

10-11 15:25