简体   繁体   English

将 KeyValuePairs 列表传递给 LINQ 查询并选择与实体 ID 匹配的键的值

[英]Passing List of KeyValuePairs to LINQ query and selecting the Value for the Key that matches the entity's Id

What is the best way to solve this?解决这个问题的最佳方法是什么?

TL;DR version: TL;DR 版本:

Using LINQ and EntityFramework I am trying to pass in a List of KeyValuePair s to a LINQ query, then for each entity who's ID matches any Key in the list, selecting a new column with that key's value.使用 LINQ 和 EntityFramework 我试图将KeyValuePairList传递给 LINQ 查询,然后对于 ID 与列表中的任何键匹配的每个实体,选择具有该键值的新列。

So if my KeyValuePair s are所以如果我的KeyValuePair

List<KeyValuePair(int, long)> keyValuePairs = new() 
{
    new(1, 10),
    new(2, 20),
    new(3, 30)
}

where the key is an EmployeeId and the value is a CompanyId ,其中键是EmployeeId ,值是CompanyId

and I'm querying an Employees table, with two columns, Id and Name ,我正在查询一个Employees表,它有两列, IdName

and my EmployeeModel class is我的EmployeeModel class 是

public int Id;
public int Name;
public long CompanyId;

I want to do something like this:我想做这样的事情:

var keys = keyValuePairs.Select(x => x.Key);

IQueryable<EmployeeModel> EmployeeModels = Db.Employees
    .Where(e => keys.Contains(p.Id))
    .Select(e => new EmployeeModel 
    {
        Id = e.Id,
        Name = e.Name,
        CompanyId = keyValuePairs.Where(x => x.Key == e.Id).Select(x => x.Value).First()
    });

Currently I get this exception:目前我得到这个例外:

The LINQ expression 'x' could not be translated.无法翻译 LINQ 表达式“x”。 Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.以可翻译的形式重写查询,或通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端评估。

I want to avoid loading this into memory because the results could potentially be hundreds of thousands.我想避免将其加载到 memory 中,因为结果可能有数十万。 In addition, I need to pass the result into a method that requires an IQueryable另外,我需要将结果传递给需要 IQueryable 的方法

More context for understanding what I'm trying to do (and maybe someone can see if this is an xy problem):了解我正在尝试做什么的更多上下文(也许有人可以看到这是否是 xy 问题):

I have two Databases Database A has a Table called Companies and a join table called CompanyEmployees我有两个数据库数据库 A 有一个名为 Companies 的表和一个名为 CompanyEmployees 的连接表

Database B has a Table called Employees The Employee table has a column for Employee Id, but no column for Company Id数据库 B 有一个名为 Employees 的表 Employee 表有一个 Employee Id 列,但没有 Company Id 列

My EmployeeModel class however, has a prop for the CompanyId of the company to which the employee belongs.但是,我的EmployeeModel class 具有员工所属公司的CompanyId的道具。 I'm trying to load a list of employees, and selecting a EmployeeModel which includes the CompanyId我正在尝试加载员工列表,并选择包含CompanyIdEmployeeModel

The way I'm trying to accomplish that is loading into memory a List of KeyValuePair s where the Key = CustomerEmployee.EmployeeId and the Value = CustomerEmployee.CustomerId and then using that List to Query the other Db.我试图完成的方法是将KeyValuePair的列表加载到 memory 中,其中Key = CustomerEmployee.EmployeeIdValue = CustomerEmployee.CustomerId ,然后使用该列表查询另一个 Db。 (I don't see any alternative to loading the List of KeyValuePair s into memory, even though I'd obviously rather not) (我没有看到将KeyValuePair列表加载到 memory 的任何替代方法,即使我显然不想这样做)

Also you probably have a problem with this part:你也可能对这部分有问题:

CompanyId = keyValuePairs.Where(x => x.Key == e.Id).Select(x => x.Value)

As CompanyId is a long, and the result of your Linq expression is a IEnumerable.由于 CompanyId 很长,并且您的 Linq 表达式的结果是 IEnumerable。 If you're sure you have a result and only one, then you may use:如果您确定只有一个结果,那么您可以使用:

CompanyId = keyValuePairs.Where(x => x.Key == e.Id).Select(x => x.Value).First()

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

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