C# .NET列表
我正在使用.NET3.5。为什么我仍然得到: 不包含“独特”的定义 使用此代码:C# .NET列表,c#,linq,.net-3.5,extension-methods,C#,Linq,.net 3.5,Extension Methods,我正在使用.NET3.5。为什么我仍然得到: 不包含“独特”的定义 使用此代码: using System.Collections.Generic; //.. . . . . code List<string> Words = new List<string>(); // many strings added here . . . Words = Words.Distinct().ToList(); 使用System.C
using System.Collections.Generic;
//.. . . . . code
List<string> Words = new List<string>();
// many strings added here . . .
Words = Words.Distinct().ToList();
使用System.Collections.Generic;
//.. . . . . 代码
列表单词=新列表();
//这里添加了许多字符串。
单词=单词.Distinct().ToList();
你在吗
using System.Linq;
?
Distinct
是在System.Linq.Enumerable
中定义的扩展方法,因此需要使用语句添加该方法
不要忘了添加对System.Core.dll的引用(如果您使用的是VS2008,这已经为您完成了)。您忘了添加
using System.Linq;
Distinct
是中定义的,因此只有在导入该命名空间时才能调用它
您还需要添加对System.Core.dll
的引用
如果您将该项目创建为.Net 3.5项目,则该项目已被引用;如果是从.Net 2或3升级的,则必须自己添加引用。List words=new List();
List<string> words = new List<string>();
// many strings added here . . .
IEnumerable <string> distinctword =Words .distinct();
foreach(string index in distinctword )
{
// do what u want here . . .
}
//这里添加了许多字符串。
IEnumerable distinctword=Words.distinct();
foreach(distinctword中的字符串索引)
{
//在这里做你想做的。
}
来自msdn博客:Charlie Calvert
用于:
--项目类型:控制台
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var listA = new List<int> { 1, 2, 3, 3, 2, 1 };
var listB = listA.Distinct();
foreach (var item in listB)
{
Console.WriteLine(item);
}
}
}
// output: 1,2,3
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{
公共静态void Main()
{
Console.WriteLine(“你好世界”);
var listA=新列表{1,2,3,3,2,1};
var listB=listA.Distinct();
foreach(列表B中的var项)
{
控制台写入线(项目);
}
}
}
//产量:1,2,3
这样命名有什么原因吗?Martinho——命名什么像什么?System.Core。主要是。。。那为什么是核心呢?啊。我不知道有什么特别好的理由。你会给它取什么名字?我想这里的“Core”意思是“应该在mscorlib中的东西,但由于backcompat的原因,我们不能把它放在那里”:)提供修复并不能回答“为什么”的问题。