是的,所以我有一个可枚举的,并希望从中获得不同的值.
使用System.Linq,当然有一个名为的扩展方法Distinct.在简单的情况下,它可以在没有参数的情况下使用,例如:
var distinctValues = myStringList.Distinct();
Run Code Online (Sandbox Code Playgroud)
好的,但如果我有一个可以指定相等性的可枚举对象,唯一可用的重载是:
var distinctValues = myCustomerList.Distinct(someEqualityComparer);
Run Code Online (Sandbox Code Playgroud)
equality comparer参数必须是.的实例IEqualityComparer<T>.当然,我可以做到这一点,但它有点冗长,而且很有说服力.
我所期望的是一个需要lambda的重载,比如Func <T,T,bool>:
var distinctValues
= myCustomerList.Distinct((c1, c2) => c1.CustomerId == c2.CustomerId);
Run Code Online (Sandbox Code Playgroud)
任何人都知道是否存在某些此类扩展或某些等效的解决方法?或者我错过了什么?
或者,有没有一种方法可以指定IEqualityComparer内联(embarass me)?
更新
我找到了Anders Hejlsberg对MSDN论坛中关于这个主题的帖子的回复.他说:
您将遇到的问题是,当两个对象比较相等时,它们必须具有相同的GetHashCode返回值(否则Distinct内部使用的哈希表将无法正常运行).我们使用IEqualityComparer,因为它将Equals和GetHashCode的兼容实现打包到一个接口中.
我认为那是有道理的..
我有一个集合:
List<Car> cars = new List<Car>();
Run Code Online (Sandbox Code Playgroud)
汽车是由他们的财产唯一标识CarCode.
我收集了三辆汽车,两辆汽车编码相同.
如何使用LINQ将此集合转换为具有唯一CarCodes的Cars?
我有一个Person对象列表.我想转换为一个字典,其中键是名字和姓氏(连接),值是Person对象.
问题是我有一些重复的人,所以如果我使用这个代码会爆炸:
private Dictionary<string, Person> _people = new Dictionary<string, Person>();
_people = personList.ToDictionary(
e => e.FirstandLastName,
StringComparer.OrdinalIgnoreCase);
Run Code Online (Sandbox Code Playgroud)
我知道这听起来很奇怪,但我现在并不关心重复的名字.如果有多个名字我只想抓一个.无论如何,我可以在上面写这个代码,所以它只需要一个名字,并没有炸毁重复?
class Program
{
static void Main(string[] args)
{
List<Book> books = new List<Book>
{
new Book
{
Name="C# in Depth",
Authors = new List<Author>
{
new Author
{
FirstName = "Jon", LastName="Skeet"
},
new Author
{
FirstName = "Jon", LastName="Skeet"
},
}
},
new Book
{
Name="LINQ in Action",
Authors = new List<Author>
{
new Author
{
FirstName = "Fabrice", LastName="Marguerie"
},
new Author
{
FirstName = "Steve", LastName="Eichert"
},
new Author
{
FirstName = "Jim", LastName="Wooley"
},
}
}, …Run Code Online (Sandbox Code Playgroud) 我有一个List<MyClass> someList.
class MyClass
{
public int Prop1...
public int Prop2...
public int Prop3...
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何获得一个新的唯一List<MyClass> distinctList的List<MyClass> someList,但它只是比较Prop2.
我有一个list<message>包含类型Guid和DateTime(以及其他属性)的属性.我想摆脱所有在列表中的项目,其中的Guid和DateTime是相同的(除了一个).有时候这两个属性会与列表中的其他项目相同,但其他属性会有所不同,所以我不能只使用.Distinct()
List<Message> messages = GetList();
//The list now contains many objects, it is ordered by the DateTime property
messages = from p in messages.Distinct( what goes here? );
Run Code Online (Sandbox Code Playgroud)
这就是我现在所拥有的,但似乎应该有更好的方法
List<Message> messages = GetList();
for(int i = 0; i < messages.Count() - 1) //use Messages.Count() -1 because the last one has nothing after it to compare to
{
if(messages[i].id == messages[i+1}.id && messages[i].date == message[i+1].date)
{
messages.RemoveAt(i+1);
{
else
{ …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
var foo = (from data in pivotedData.AsEnumerable()
select new
{
Group = data.Field<string>("Group_Number"),
Study = data.Field<string>("Study_Name")
}).Distinct();
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,这将返回不同 但是,我想要的是返回强类型集合而不是匿名类型,所以当我这样做时:
var foo = (from data in pivotedData.AsEnumerable()
select new BarObject
{
Group = data.Field<string>("Group_Number"),
Study = data.Field<string>("Study_Name")
}).Distinct();
Run Code Online (Sandbox Code Playgroud)
这不会返回不同的值,而是返回所有值.有没有办法用实际对象做到这一点?
我正在编写一个函数,它使用LINQ从DataBase中获取记录以获得IQueryable.此LINQ语句将在特定时间段内为活动用户提取所有记录,然后将UserID,First Name和Last Name吐出到Telerik RadGrid.
我的问题在于在拉动此数据时尝试在UserID上获得一个独特的值.我尝试重新编写此代码以获取结果.以下是拉动所有数据的代码示例,其中Distinct NOT工作.
public static IQueryable GetActiveEmployees_Grid(string Period)
{
DataContext Data = new DataContext();
var Employees = (from c in DataSystem_Records
where c.Period == Period
orderby c.DataSystem_Employees.LName
select c).Distinct();
return Employees;
}
Run Code Online (Sandbox Code Playgroud)
将DataSource应用到我的网格后,这将返回用户4次,该句点的每个记录一个实例.

有没有办法将Distinct应用于我的LINQ函数以使其按照我打算的方式工作?
我有一些对象:
class Foo {
public Guid id;
public string description;
}
var list = new List<Foo>();
list.Add(new Foo() { id = Guid.Empty, description = "empty" });
list.Add(new Foo() { id = Guid.Empty, description = "empty" });
list.Add(new Foo() { id = Guid.NewGuid(), description = "notempty" });
list.Add(new Foo() { id = Guid.NewGuid(), description = "notempty2" });
Run Code Online (Sandbox Code Playgroud)
我想以这样一种方式处理这个列表,即该id字段是唯一的,并抛弃非唯一对象(基于id).
我能想到的最好的是:
list = list.GroupBy(i => i.id).Select(g=>g.First()).ToList();
Run Code Online (Sandbox Code Playgroud)
是否有更好/更好/更快的方法来实现相同的结果.
我收到一个实体框架类型列表,并希望只返回List中的不同值.我正在使用以下方法,但它并没有统一列表.有什么建议?
帕拉姆: List<Flag> flags
List<Flag> distinctFlags = flags.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
Flag的值如下:ID,Flag,FlagValue.我可以在这个例子中使用linq吗?
谢谢.
c# ×10
linq ×8
.net ×3
distinct ×3
list ×2
c#-3.0 ×1
class ×1
dictionary ×1
iequatable ×1
lambda ×1
properties ×1