gpt4 book ai didi

c# - 加入 LINQ 查询语法 : moving right side to the left

转载 作者:太空狗 更新时间:2023-10-30 01:28:53 26 4
gpt4 key购买 nike

我有一个场景,我需要连接两个表:

一个

|---------------------|------------------|
| ID | Name |
|---------------------|------------------|
| 1 | John |
|---------------------|------------------|
| 2 | Matt |
|---------------------|------------------|
| 3 | Emma |
|---------------------|------------------|

B

|---------------------|------------------|
| ID | Text |
|---------------------|------------------|
| 1 | blah blah John |
|---------------------|------------------|
| 2 | this is some data|
|---------------------|------------------|
| 3 | My name is Jeff |
|---------------------|------------------|

我需要使用 LINQ 的查询语法来连接这两个表。

左边的表需要是表A。

虽然我需要根据“文本”列是否包含表 A 中名称列中的文本进行联接。

代码应该是这样的:

var result = from ta in A
join tb in B on tb.Text.Contains(ta.Name)

我似乎无法在联接的左侧使用 tb

我只能使用ta

tb 在联接的右侧工作。

有什么方法可以切换它,以便我可以在左侧使用 tb 吗?

最佳答案

LINQ 中的联接只能使用相等匹配。但是您可以将 SelectMany 方法与 Where 结合使用。它基本上会按条件进行连接。

在查询语法中,它会是这样的:

from ta in A
from tb in B
where tb.Text.Contains(ta.Name)
// the rest of your query

另见 Perform custom join operations

关于c# - 加入 LINQ 查询语法 : moving right side to the left,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55742864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com