LINQ查询来搜索元组 [英] LINQ query that searches for tuples

查看:35
本文介绍了LINQ查询来搜索元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如

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

是否有可能编写一个LINQ查询,该查询实质上执行以下SQL查询的工作并返回NameAttribX的元组?

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')

输入中约有1000个元组.

There are about a 1000 tuples in the input.

推荐答案

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

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))

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

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. :)

这篇关于LINQ查询来搜索元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆