是的,所以我有一个可枚举的,并希望从中获得不同的值.
使用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的兼容实现打包到一个接口中.
我认为那是有道理的..
我的问题被标记为这个问题的可能重复:如何在没有循环的情况下组合两个词典?
我相信我的问题是不同的,因为我问的是如何以特定的方式组合两个词典:我希望Dictionary1中的所有项目加上Dictionary2中不存在的所有项目(即密钥不存在).
我有两个这样的词典:
var d1 = new Dictionary<string,object>();
var d2 = new Dictionary<string,object>();
d1["a"] = 1;
d1["b"] = 2;
d1["c"] = 3;
d2["a"] = 11;
d2["e"] = 12;
d2["c"] = 13;
Run Code Online (Sandbox Code Playgroud)
我想将它们组合成一个新的词典(从技术上讲,它不必是一个字典,它可能只是一个序列KeyValuePairs),以便输出包含所有KeyValuePairs来自d1和只有KeyValuePairs,d2其Key不会出现在d1.
概念:
var d3 = d1.Concat(d2.Except(d1))
Run Code Online (Sandbox Code Playgroud)
但这给了我d1和d2的所有元素.
似乎它应该是显而易见的,但我必须遗漏一些东西.
我有一些对象:
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)
是否有更好/更好/更快的方法来实现相同的结果.
TL; DR我正在寻找一种方式来获得IEqualityComparer<T>的IComparer<T>,无论哪个数据类型是T,包括在不区分大小写的选项T是string.或者我需要针对此问题的不同解决方案.
以下是完整的故事:我正在使用LFU策略实现简单的通用缓存.要求是必须能够选择缓存是区分大小写还是不区分大小写 - 如果string恰好是缓存键的数据类型(这不是必需的).在解决方案中,我主要开发缓存,我期望数百亿的缓存查找,以及最多100,000个条目的缓存大小.由于这些数字,我立即停止使用导致分配的任何字符串操作(例如.ToLower().GetHashCode()等),而是选择使用IComparer和IEqualityComparer,因为它们是标准的BCL功能.此缓存的用户可以将比较器传递给构造函数.以下是代码的相关片段:
public class LFUCache<TKey,TValue>
{
private readonly Dictionary<TKey,CacheItem> entries;
private readonly SortedSet<CacheItem> lfuList;
private class CacheItem
{
public TKey Key;
public TValue Value;
public int UseCount;
}
private class CacheItemComparer : IComparer<CacheItem>
{
private readonly IComparer<TKey> cacheKeyComparer;
public CacheItemComparer(IComparer<TKey> cacheKeyComparer)
{
this.cacheKeyComparer = cacheKeyComparer;
if (cacheKeyComparer == null)
this.cacheKeyComparer = Comparer<TKey>.Default;
}
public int Compare(CacheItem x, CacheItem y) …Run Code Online (Sandbox Code Playgroud) 我尝试将 except 方法与自定义相等比较器一起使用,但它不起作用。
我的平等比较器:
public class BusinessObjectGuidEqualityComparer<T> : IEqualityComparer<T> where T : BusinessObject
{
#region IEqualityComparer<T> Members
/// <summary>
/// Determines whether the specified objects are equal.
/// </summary>
/// <param name="x">The first object of type <paramref name="T"/> to compare.</param>
/// <param name="y">The second object of type <paramref name="T"/> to compare.</param>
/// <returns>
/// <see langword="true"/> If the specified objects are equal; otherwise, <see langword="false"/>.
/// </returns>
public bool Equals(T x, T y)
{
return (x == null && y …Run Code Online (Sandbox Code Playgroud) 因此,我已经在SO和其他地方查看了大约20个例子,但是没有找到一个涵盖我正在尝试做的事情.这个 - 我可以指定我的显式类型比较器内联吗? - 看起来像我需要的,但不够远(或者我不明白如何进一步采取它).
需要(我认为)提供自定义IEqualityComparer来指定如何比较GroupBy字段,但它们是匿名类型
private class LoadData
{
public PeriodEndDto PeriodEnd { get; set; }
public ComponentDto Component { get; set; }
public string GroupCode { get; set; }
public string PortfolioCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)我到目前为止最好的GroupBy查询:
var distinctLoads = list.GroupBy(
dl => new { PeriodEnd = dl.PeriodEnd,
Component = dl.Component,
GroupCode = dl.GroupCode },
(key, data) => new {PeriodEnd = key.PeriodEnd,
Component = key.Component,
GroupCode = key.GroupCode,
PortfolioList = data.Select(d=>d.PortfolioCode)
.Aggregate((g1, g2) => g1 …Run Code Online (Sandbox Code Playgroud) 我遇到了Except()方法的麻烦.它不返回差异,而是返回原始集合.
我已经尝试在Account类中实现IEquatable和IEqualityComparer.我也尝试为Account创建一个单独的IEqualityComparer类.
当从main调用Except()方法时,它似乎不会调用我的自定义Equals()方法,但是当我尝试Count()时,它确实调用了自定义的GetHashCode()方法!
我确定我在某个地方犯了一个小错误,我希望一双新眼睛可以帮助我.
主要:
IEnumerable<Account> everyPartnerID =
from partner in dataContext.Partners
select new Account { IDPartner = partner.ID, Name = partner.Name };
IEnumerable<Account> hasAccountPartnerID =
from partner in dataContext.Partners
from account in dataContext.Accounts
where
!partner.ID.Equals(Guid.Empty) &&
account.IDPartner.Equals(partner.ID) &&
account.Username.Equals("Special")
select new Account { IDPartner = partner.ID, Name = partner.Name };
IEnumerable<Account> noAccountPartnerID =
everyPartnerID.Except(
hasAccountPartnerID,
new LambdaComparer<Account>((x, y) => x.IDPartner.Equals(y.IDPartner)));
Run Code Online (Sandbox Code Playgroud)
帐户:
public class Account : IEquatable<Account>
{
public Guid IDPartner{ get; set; }
public string Name{ get; set; } …Run Code Online (Sandbox Code Playgroud) 我无法想象从一个struct数组中删除duplicates条目
我有这个结构:
public struct stAppInfo
{
public string sTitle;
public string sRelativePath;
public string sCmdLine;
public bool bFindInstalled;
public string sFindTitle;
public string sFindVersion;
public bool bChecked;
}
Run Code Online (Sandbox Code Playgroud)
我已经改变了stAppInfo结构类在这里感谢乔恩斯基特
代码是这样的:(简短版)
stAppInfo[] appInfo = new stAppInfo[listView1.Items.Count];
int i = 0;
foreach (ListViewItem item in listView1.Items)
{
appInfo[i].sTitle = item.Text;
appInfo[i].sRelativePath = item.SubItems[1].Text;
appInfo[i].sCmdLine = item.SubItems[2].Text;
appInfo[i].bFindInstalled = (item.SubItems[3].Text.Equals("Sí")) ? true : false;
appInfo[i].sFindTitle = item.SubItems[4].Text;
appInfo[i].sFindVersion = item.SubItems[5].Text;
appInfo[i].bChecked = (item.SubItems[6].Text.Equals("Sí")) ? true : false;
i++;
}
Run Code Online (Sandbox Code Playgroud)
我需要 …
我有几个包含大量重复条目的XML文件,例如这些.
<annotations>
<annotation value=",Clear,Outdoors" eventID="2">
<image location="Location 1" />
<image location="Location 2" />
<image location="Location 2" />
</annotation>
<annotation value=",Not a problem,Gravel,Shopping" eventID="2">
<image location="Location 3" />
<image location="Location 4" />
<image location="Location 5" />
<image location="Location 5" />
<image location="Location 5" />
</annotation>
</annotations>
Run Code Online (Sandbox Code Playgroud)
我想删除每个子节点中的重复元素.我接近这个的方法是将所有元素复制到列表然后比较它们,
foreach (var el in xdoc.Descendants("annotation").ToList())
{
foreach (var x in el.Elements("image").Attributes("location").ToList())
{
//add elements to a list
}
}
Run Code Online (Sandbox Code Playgroud)
一半我意识到这是非常低效和耗时的.我对XML很新,我想知道C#中是否有任何内置方法可以用来删除重复项?
我试过用
if(!x.value.Distinct()) // can't convert collections to bool
x.Remove();
Run Code Online (Sandbox Code Playgroud)
但这不起作用,也不起作用
if(x.value.count() > 1) // …Run Code Online (Sandbox Code Playgroud) 我有一个具有属性名称和ID的类Employee
我有一个数组Employee []一个另一个数组Employee [] B.如何比较两个数组并从A中删除B中不存在的值?
c# ×10
linq ×5
.net ×4
c#-4.0 ×2
c#-3.0 ×1
dictionary ×1
group-by ×1
icomparer ×1
iequatable ×1
lambda ×1
linq-to-sql ×1
memorycache ×1
xml ×1