简体   繁体   English

将Vb.Net Linq转换为C#但不同的错误

[英]Converting Vb.Net Linq to C# but Distinct Error

I am trying to move some of my code over from vb.net to c# class library. 我试图将我的一些代码从vb.net移动到c#类库。 I have linq query in vb.net that is querying against a DataTable. 我在vb.net中有一个查询DataTable的linq查询。

Dim qryUser = From db In dt.AsEnumerable() _
              Select userName = db.Field(Of String)("UserName") Distinct
              Order By userName Ascending

Now I am trying to make this work in C# and I have 现在我想用C#做这个工作而且我有

IEnumerable<string> qryUser = (from db in Table.AsEnumerable()
                               orderby db.Field<string>("UserName") ascending
                               select db.Field<string>("UserName"));

Now all examples I have found searching, shows I should just have to add 现在我找到的所有搜索示例都显示我应该添加

.Distinct()

to the end of my c# linq query to make it look like this 到我的c#linq查询的末尾,使它看起来像这样

IEnumerable<string> qryUser = (from db in Table.AsEnumerable()
                               orderby db.Field<string>("UserName") ascending
                               select db.Field<string>("UserName")).Distinct()

but doing this gives me an error in my IDE 但这样做会让我在IDE中出错

Error 1 'System.Data.EnumerableRowCollection' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'System.Data.EnumerableRowCollection' could be found 错误1'System.Data.EnumerableRowCollection'不包含'Distinct'的定义,并且没有扩展方法'Distinct'接受类型'System.Data.EnumerableRowCollection'的第一个参数可以找到

Can someone, please, show me the proper way of doing this? 有人可以告诉我这样做的正确方法吗? Thanks in advance. 提前致谢。

I was able to copy/paste your code (and setup a small DataTable ) and it worked fine. 我能够复制/粘贴您的代码(并设置一个小的DataTable ),它工作正常。

The Distinct extension method lives in the System.Linq namespace. Distinct扩展方法位于System.Linq命名空间中。

Make sure the following using directive is at the top of your class file: 确保以下using指令位于类文件的顶部:

using System.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM