简体   繁体   English

检查 ID 列表是否有效的更好方法(EF Core)

[英]Better way to check if a list of IDs is valid (EF Core)

I have a list of checkbox elements in a html page, all selectable and with distinct IDs, and its IDs are send by HTTP POST to my API.我在 html 页面中有一个复选框元素列表,所有这些元素都是可选的并且具有不同的 ID,它的 ID 通过 HTTP POST 发送到我的 API。 For security, I need to check if any of the sent IDs is invalid.为了安全起见,我需要检查是否有任何发送的 ID 无效。 It is just a method that returns false if any of those IDs does not exists in my database, and returns true if all of those exists.它只是一种方法,如果我的数据库中不存在这些 ID 中的任何一个,则返回 false,如果所有这些 ID 都存在,则返回 true。

It looked easy initially, but I didn't find a way to return that result from my database directly using EF Core.最初看起来很简单,但我没有找到直接使用 EF Core 从我的数据库返回该结果的方法。 I always need to compare the Count of my query result with my list Count .我总是需要将我的查询结果的Count与我的列表Count

My better approach was:我更好的方法是:

public async Task<bool> IsIdListValid(IEnumerable<int> idList) =>
   (await _context.Foo
              .Select(x => x.Id)
              .CountAsync(id => idList.Contains(id))
   ) == idList.Distinct().Count();

So, what I'm asking is: there is a better way to write that query, more readable and performant?所以,我要问的是:有没有更好的方法来编写该查询,更具可读性和性能?

What you are looking for is All :您正在寻找的是All

public async Task<bool> IsIdListValid(IEnumerable<int> idList)  
{
   var validIds = await _context.Foo.Select(x => x.Id).ToListAsync();
   return idList.All(x => validIds.Contains(x));
}

Instead of verifying by count, you can implement your question directly:您可以直接实施您的问题,而不是按计数进行验证:

public async Task<bool> IsIdListValid(IEnumerable<int> idList) =>
    idList.Any(id => !_context.Foo.Any(f => f.id == id));

This will send on SQL query per member of idList when it is valid.这将在idList每个成员有效时发送 SQL 查询。 If idList is short, this might be okay, otherwise your Count query is probably best, though the Select isn't needed.如果idList很短,这可能没问题,否则您的Count查询可能是最好的,尽管不需要Select This will shortcut however if idList contains an invalid member, stopping on the first invalid member.然而,如果idList包含无效成员,这将是快捷方式,在第一个无效成员上停止。

Another possiblity is using the EntityFrameworkCore.MemoryJoin extension that adds the ability to send an in-memory list using SQL VALUES and then join to it.另一种可能性是使用EntityFrameworkCore.MemoryJoin扩展,它增加了使用 SQL VALUES发送内存列表然后加入它的能力。

assume you have list of ids with name 'idsLst', so you will get all matched ids from db and then compare the returned id list returned from db with your list if they are identical, so all list items are exist in db, if there's diff, it's mean you have some invalid Ids假设您有名称为“idsLst”的 id 列表,因此您将从 db 中获取所有匹配的 id,然后将从 db 返回的返回 id 列表与您的列表进行比较(如果它们相同),因此所有列表项都存在于 db 中,如果有差异,这意味着您有一些无效的 ID

var validIds = DbContext
    .UREntity
    .Where(x => idsLst.Contains(x.Id))
    .Select(x => x.Id);
    

var invalidIdsLst = ids.Except(validIds);

return invalidIdsLst.Count == 0; // return true if all valid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM