LINQ / VB.NET 选择与数据集不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3535283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ / VB.NET select distinct from dataset
提问by dferraro
I have a single columned datatable inside a single tabled dataset.
我在单个表格数据集中有一个单列数据表。
I just want to convert this dataset to distinct rows. Here is my code, it gives compile error '.' expected. What am I doing wrong? (I tried adding the ., still same error). I know this is something stupidly obvious. PLZ Save me! ;)
我只想将此数据集转换为不同的行。这是我的代码,它给出了编译错误“。” 预期的。我究竟做错了什么?(我尝试添加 .,仍然是同样的错误)。我知道这是一件非常明显的事情。LZ救救我!;)
Thanks much in advance!
非常感谢!
Dim query = _
From email In ds.Tables(0) _
Select email.Field<string>("Email").Distinct()
EDIT: DOH! MIXING VB/C# SYNTAX HERE! I changed to (Of String) and it works... BUT NOW 'query' is an ienumerable collection of characters... not a datatable... so how do I convert back easily without manually doing a loop?? Plz advise!
编辑:DOH!在这里混合 VB/C# 语法!我改为 (Of String) 并且它有效......但现在'查询'是一个可枚举的字符集合......不是数据表......那么我如何在不手动执行循环的情况下轻松转换回来?请各位指教!
回答by Guffa
You are applying the Distinct
method to each string, not the result of the query. As strings are collection of character, you can apply extension methods to them too.
您将该Distinct
方法应用于每个字符串,而不是查询的结果。由于字符串是字符的集合,您也可以对它们应用扩展方法。
Put parentheses around the query:
将括号括在查询中:
Dim query = _
(From email In ds.Tables(0) _
Select email.Field(Of String)("Email")).Distinct()
回答by Rami Issa
You can use the distinct values after getting the values into datatabale by:
通过以下方式将值放入数据表后,您可以使用不同的值:
dtFiltered = dtNotFiltered.DefaultView.ToTable(True, "RAMI_ISSA") 'Get distinct values (by stting the first parameter to True)