搜索图元的LINQ查询[英] LINQ query that searches for tuples

本文是小编为大家收集整理的关于搜索图元的LINQ查询的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个 Fname 和 Version 的元组 (List<Tuple<string,string>>) 列表作为输入.

例如.

[('firstname1','1.0.1'), ('firstname2','2.3.3'), ('firstname3','4.4.1')] 

是否可以编写一个 LINQ 查询,该查询基本上执行以下 SQL 查询的操作并返回 Name 和 AttribX 的元组?

SELECT e.Name, a.AttribX
FROM Element e
JOIN Attributes a ON a.AId=e.EAId
where (a.FName='firstname1' and a.Version='1.0.1')
   OR (a.Fname='firstname2' and a.Version='2.3.3')
   OR (a.Fname='firstname3' and a.Version='4.4.1')

输入中有大约 1000 个元组.

推荐答案

您的 Where 子句可能是(如果使用 LINQ to Objects):

var results = yourData.Where(z => yourListOfTuples.Contains(new Tuple<string, string>(z.FirstName, z.Version)))

另一个尝试的选项(针对实体框架):

var tuples = yourListOfTuples.Select(z => z.Item1 + "-" + z.Item2).ToList();
var results = yourData.Where(z => tuples.Contains(z.FirstName + "-" + z.Version))

第二个代码示例只是连接两个字段 - 这将对数据库查找产生负面影响(因为它可能必须进行扫描而不是查找).例如,如果 FirstName 或 LastName 包含 -,您也可能会遇到问题.从好的方面来说,它将只使用 1000 个参数而不是 2000 个.:)

本文地址:https://www.itbaoku.cn/post/1556759.html

问题描述

I have a list of tuples (List<Tuple<string,string>>) of Fname and Version as input.

eg.

[('firstname1','1.0.1'), ('firstname2','2.3.3'), ('firstname3','4.4.1')] 

Is it possible to write a LINQ query that essentially does what the following SQL query does and returns tuples of Name and AttribX?

SELECT e.Name, a.AttribX
FROM Element e
JOIN Attributes a ON a.AId=e.EAId
where (a.FName='firstname1' and a.Version='1.0.1')
   OR (a.Fname='firstname2' and a.Version='2.3.3')
   OR (a.Fname='firstname3' and a.Version='4.4.1')

There are about a 1000 tuples in the input.

推荐答案

Your Where clause could be (if using LINQ to Objects):

var results = yourData.Where(z => yourListOfTuples.Contains(new Tuple<string, string>(z.FirstName, z.Version)))

Another option to try (against Entity Framework):

var tuples = yourListOfTuples.Select(z => z.Item1 + "-" + z.Item2).ToList();
var results = yourData.Where(z => tuples.Contains(z.FirstName + "-" + z.Version))

The second code sample just concatenates the two fields - this will negatively impact database lookups (since it will likely have to do scans rather than seeks). You may also have issues if FirstName or LastName contains - for example. On the upside it will use only 1000 parameters rather than 2000. :)