Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用EF Core 2.1从数据库中不存在的输入列表中获取项目_C#_Linq_Entity Framework Core - Fatal编程技术网

C# 使用EF Core 2.1从数据库中不存在的输入列表中获取项目

C# 使用EF Core 2.1从数据库中不存在的输入列表中获取项目,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,我想从输入列表中获取数据库表中不存在的项 我传递了一个id列表,然后我想返回那些在我的表中不存在的id 这就是我现在所拥有的: var input = new List<string>() // list of Ids, for example count of 10 var itemsThatExistInDb = await DbContext.Set<Data>() // in table exist 100k+ records, .AsQuery

我想从输入列表中获取数据库表中不存在的项

我传递了一个
id
列表,然后我想返回那些在我的表中不存在的
id

这就是我现在所拥有的:

var input = new List<string>() // list of Ids, for example count of 10

var itemsThatExistInDb = await DbContext.Set<Data>() // in table exist 100k+ records,
        .AsQueryable()                               // can't use simple !Contains()
        .Where(x => input.Contains(x.Id))
        .Select(x => x.Id)
        .ToListAsync();

var itemsThatNotExistInDb = input.Except(itemsThatExistInDb).ToList();
var input=new List()//ID列表,例如计数10
var itemsThatExistInDb=await DbContext.Set()//表中存在100k+条记录,
.AsQueryable()//无法使用simple!包含()
.Where(x=>input.Contains(x.Id))
.选择(x=>x.Id)
.ToListAsync();
var itemsThatNotExistInDb=input.Except(itemsThatExistInDb.ToList();

如何在EF Core 2.1中编写查询,从我的输入列表中获取数据库中不存在的项,而不使用linq扩展,如
Except()
?如果可能的话,我想直接从数据库查询中获取这些
id
DbContext

我想您可以这样查询:

var input = new List<string>() // list of Ids

       List<string> itemsThatExist = new List<string>();

       var itemsThatNotExist  = await DbContext.Set<Data>()
                .AsQueryable()
                .ToDictionaryAsync(_ => _.ID, _ => _);

        foreach (var item in input)
        {
            if(AllItems.ContainsKey(item)){
                itemsThatExist.Add(item);
                itemsThatNotExist.Remove(item);
            }
        }
var input=new List()//ID列表
List itemsThatExist=新列表();
var itemsThatNotExist=await DbContext.Set()
.AsQueryable()
.ToDictionaryAsync(=>u.ID,=>u);
foreach(输入中的变量项)
{
if(所有项目集装箱(项目)){
itemsThatExist。添加(项);
itemsThatNotExist.Remove(项目);
}
}

如果您不想使用
!包含
您可以使用如下逻辑方法:

datatTable是表的内容

List<string> fullList = new List<string>() { "3", "1", "2" }; //or any dynamic list content or class list based on requirement.
List<string> itemsThatNotExistInDb = new List<string>(); //New container list same as fullList
            foreach (var item in fullList)
            {
                var isRemoved = dataTable.RemoveAll(a => a.Id == item) == 1 ? true : false;
                if (isRemoved)
                {
                    itemsThatNotExistInDb.Add(item);
                }
            }
List fullList=newlist(){“3”、“1”、“2”}//或任何基于需求的动态列表内容或类列表。
List itemsThatNotExistInDb=新列表()//新容器列表与完整列表相同
foreach(完整列表中的变量项)
{
var isRemoved=dataTable.RemoveAll(a=>a.Id==item)==1?true:false;
如果(已删除)
{
itemsThatNotExistInDb.Add(项目);
}
}

什么是
输入
?某种数组或列表?它的
ID列表
。我的表中的Id是string,我不能使用simple
!input.Contains(x.Id)
因为我将把整个表提取到内存中(例如,如果我想从input中检查10个Id,并且当我在表中有来自!Contains()的100k条记录时,我将从中获取其余的99990条记录),你能写原始sql查询吗?如果是,则在输入列表和db表之间进行左连接,输入列表位于左侧。返回右边为NULL的输入ID。您可以检查。这将非常慢。查看我在原始帖子下的评论。我们不能使用
!Contains()
因为它可以将整个表下载到内存中(您的表有100k条记录,而输入列表有10条记录),所以您总共必须下载整个表=>AllItems=ItemsThatExist+ItemsThatNotExist。因此,将所有项作为字典查询,然后迭代Dict并将其添加到itemsThatExist和itemsThatNotExist。