2013-09-20 33 views
1

我想使用LINQ to SQL根据数组中的值查询相应的数据,但遇到问题。我读这些线程,但我不知道他们是否实际上是我想要做的,如果他们是如何实现它们:如何为数组中的每个条目运行查询?

LINQ equivalent of foreach for IEnumerable<T>

Linq style "For Each"

我有我的数组字符串的行[]',我怎样才能为每个条目运行以下查询,并以一种允许我以一致的方式输出它们的方式存储结果。 再次这里是我的数组的一个示例: 例子: Z1234 Z2345 ZAF38383

//some non working code 
List<string> results = new List<string>(); 

var thisQuery = from c in myContext.SpecificTable 
where c.itemNumber == (foreach (string i in lines)) 
select c; 

foreach (var result in thisQuery) 
{ 
    results.Add(result); 
} 

列表创建是好的,我也想在写列表将是确定的,但我不能弄清楚如何为每个物品运行查询int array?

我的数组中的每个条目都以Z开头,然后包含字母数字字符的任何排列(如果它很重要)。示例:Z3333

数组中的每个条目都对应于数据库'SpecificTable'中表中的条目。我想在该表中返回与此值相关的所有内容,以便我可以输出该数据的详细信息。

例子: 我想quering Z1234开始,当Z1234在“SpecificTable”发现我希望能够输出各种细节是这样的:

foreach (var res in thisQuery) 
{ 
    //each result from the query (total of 3 from the example) will now show their Description in a messagebox. 
    MessageBox.Show("Description:" + res.Description.ToString()); 
} 

通过使用循环希望能够根据初始数组创建所有结果的列表,并输出其相应的各种值,如'说明'。

如果这还不够信息,请让我知道我可以提供更清楚。

+2

'foreach(var result in thisQuery){results.Add(result); }'可缩短为'results = thisQuery.ToList();' – nothrow

回答

1

尝试:

这将循环在myContext.SpecificTable行,并创建一个innerloop,以检查是否有行内的匹配。

var thisQuery = from c in myContext.SpecificTable 
       from i in lines 
       where c.itemNumber == i 
       select c; 

或本:

这也将这样做,只有第二“迭代”是内包含完成。

var thisQuery = from c in myContext.SpecificTable 
       where lines.Contains(c.itemNumber) 
       select c; 

我认为最好把db查询放在outerloop中,因为你不想为行中的每个项目使用'tablescan'。

所以它会做一个'桌子扫描'只有一个,并尝试找到一个匹配的行。

我想,如果你尝试这样就会加速:

var linesHashSet = new HashSet<string>(lines); 

var thisQuery = from c in myContext.SpecificTable 
       where linesHashSet.Contains(c.itemNumber) 
       select c; 

这将在第二次迭代使用一个HashSet(索引列表)


更新:

有可能迭代客户端的行,如下所示:

var thisQuery = from c in myContext.SpecificTable.ToArray() 
       where lines.Contains(c.itemNumber) 
       select c; 

但这会有一个性能差。 我建议你尝试中间的一个。

+0

当我尝试第一次查询时,它返回以下错误:本地序列不能用于除Contains运算符之外的查询运算符的LINQ to SQL实现。我真的不知道那是怎么回事。 我得到的最后一个例子,至少没有错误,当我用line'where linesHashSet.ToList()。Contains ....但我后来调用查看thisQuery的内容不产生任何结果,我想象意味着我的查询实际上没有选择任何东西? –

+0

'本地序列不能用于查询运算符的LINQ to SQL实现,但Contains运算符除外.''表示它想要为整个linq语句创建一个查询,但不能将'from i in lines'转换为sql语句。我建议你使用中间的一个。是否有可能监视生成的sql语句?我会添加一些东西给我的答案。 –

+0

关于空洞的结果,我不知道数据是怎么样的,所以我无法预测结果。 –

1

不知道我是否正确理解您的问题,但是您想要使用“lines”数组加入SpecificTable中的条目,并将结果输出为列表?

var results = myContext.SpecificTable.Select(c => lines.Contains(c.ItemNumber)).ToList(); 
foreach(var result in results) 
{ 
    Console.WriteLine(string.format("{0}:{1}", "Description", result.Description)); 
} 

如果您只想要特定属性,则可以返回一个匿名对象列表。

var results = myContext.SpecificTable.Where(c => lines.Contains(c.ItemNumber)).Select(c => new {c.ItemNumber, c.Description}).ToList(); 
foreach(var result in results) 
{ 
    Console.WriteLine(string.format("{0}:{1}", "Description", result.Description)); 
} 
+0

如果'结果'已经放入列表中,这是不是无法处理'result.Description'的最后一次调用?在我的项目中试用它并不能识别result.Description,我想知道这是为什么? –

+0

'结果'将是类型IEnumerable >我猜。找出最简单的方法是放置一个断点,并找到Select查询的返回类型。本质上,如果单个元素的类型不是你想要的,你可以提取特定的属性。你'SpecificTable'对象是否有一个Description属性? – tranceporter

相关问题