无法执行not-in

时间:2013-12-20 16:08:23

标签: c# linq

我有两个对象,一个AnotherList包含一个int数组,另一个MyFolder包含一个包含对象数组FolderItem的对象数组ItemKeyword有两个字符串(以键 - 值对样式)。

我想从MyFolder返回List<FolderItem>,在AnotherList中引用 。我可以在列表中找到 的项目列表,并在底部包含Linq。

我一直在与Contains.Except分机进行战斗,但不断出现错误。我希望这对某人来说很容易。

这是一个代码说千言万语的情况,所以在这里。

最后的Linq查询在FolderItem时仅返回一个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>();

        }
    }

}

3 个答案:

答案 0 :(得分:2)

首先获取我们不想要的所有ID,并将它们粘贴到一组中以便快速搜索:

var badIDs = new HashSet<int>(List1.items.Select(item => item.dataID));

然后获取该组中未包含的所有文件夹:

var goodFolders = List2.items.Where(folder => 
    !badIDs.Contains(folder.folderItemID));

答案 1 :(得分:1)

var res = List2.items.Where(fi => !List1.items.Any(al => 
    al.dataID.ToString() == fi.keywords.Single(k => k.key == "dataID").value));

假设FolderItem只有一个ItemKeyword key="dataID"

答案 2 :(得分:1)

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

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