我试图理解IEqualityComparer接口的GetHashCode方法的作用.
以下示例来自MSDN:
using System;
using System.Collections.Generic;
class Example {
static void Main() {
try {
BoxEqualityComparer boxEqC = new BoxEqualityComparer();
Dictionary<Box, String> boxes = new Dictionary<Box,
string>(boxEqC);
Box redBox = new Box(4, 3, 4);
Box blueBox = new Box(4, 3, 4);
boxes.Add(redBox, "red");
boxes.Add(blueBox, "blue");
Console.WriteLine(redBox.GetHashCode());
Console.WriteLine(blueBox.GetHashCode());
}
catch (ArgumentException argEx) {
Console.WriteLine(argEx.Message);
}
}
}
public class Box {
public Box(int h, int l, int w) {
this.Height = h;
this.Length = l;
this.Width = w;
}
public int …Run Code Online (Sandbox Code Playgroud) 我从这个链接有这个代码:如何删除重复的行?
;WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY person_id, date_work, hours
ORDER BY ( SELECT 0)) RN
FROM work_hours)
DELETE FROM cte
WHERE RN > 1
Run Code Online (Sandbox Code Playgroud)
是否可以删除第一个输入的重复行或我应该有一个额外的列date_of_entry?我想这样做,如果我输入相同的date_work和不同的小时,PARTITION BY person_id, date_work它删除随机重复.
如果不可能,我怎样才能删除更高时间的重复项?
我正在寻找一些帮助,使用C#/ LINQ设计查询以满足以下要求:
我有一个公司列表: -
Id Name Email Address
1 Company A a@a.com abc
2 Company B b@b.com abc
3 Company C c@c.com abc
4 Company D d@d.com abc
5 Company A a@a.com abc
Run Code Online (Sandbox Code Playgroud)
我的目标是根据两个字段检测重复项目,在此示例中为"名称"和"电子邮件".
期望的输出是如下所示的客户列表:
所需的重复清单: -
Id Qty Name Email Address
1 2 Company A a@a.com abc (Id/details of first)
2 1 Company B b@b.com abc
3 1 Company C c@c.com abc
4 1 Company D d@d.com abc
Run Code Online (Sandbox Code Playgroud) 我有一个数组MyClass,可以简化如下:
public class MyClass {
public int Id;
public string Origin;
public int Points;
public DateTime RequestTime;
public MyClass(int id, string origin, int points, DateTime requestTime) {
Id = id;
Origin = origin;
Points = points;
RequestTime = requestTime;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在Array没有来自用户端或整个输入过程的任何错误的MyClass情况下,不能存在具有相同Id和的实例Origin.
但是,如果有的话,我应该解决它.以下是解决规则:
首先Points- 也就是说,取一个具有最高权重的副本Points
但如果Points它们是相同的,我必须通过使用进一步解决它RequestTime- 最新的将被采取.
如果,没有区别RequestTime,那么我可以任意选择其中一个副本.
这是我的示例数据输入:
MyClass[] myarr = new MyClass[] {
new MyClass(1, "Ware House …Run Code Online (Sandbox Code Playgroud) 我有一个Report对象,它具有Recipients属性(String数据类型)."收件人"属性将保留所有收件人的电子邮件地址comma separated string.我需要从逗号分隔的字符串创建电子邮件对象的" 集合 ".我有以下代码使用字符串列表 来获取电子邮件地址.然后我创建了一个电子邮件对象的集合.
有没有更好的方法来避免冗余的List和Collection使用LINQ?
Report report = new Report();
report.Recipients = "test@test.com, demo@demo.com";
List<string> emailAddressList = new List<string>( report.Recipients.Split(',') );
Collection<Email> emailObjectCollection = new Collection<Email>();
foreach (string emailAddress in emailAddressList)
{
Email email = new Email();
email.EmailAddress = emailAddress;
emailObjectCollection.Add(email);
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
CA1002:不要公开通用列表.System.Collections.Generic.List是一个通用集合,专为性能而非继承而设计,因此不包含任何虚拟成员.http://msdn.microsoft.com/en-us/library/ms182142(v=vs.80).aspx
我想使用lambda表达式获得 n 个不同的字典:
Dictionary<string, string> d1 = new Dictionary<string, string>();
d1.Add("Joe", "2, Barfield Way");
d1.Add("Mike", "17, Apollo Avenue");
d1.Add("Jane", "69, Lance Drive");
Dictionary<string, string> d2 = new Dictionary<string, string>();
d2.Add("Joe", "2, Barfield Way");
d2.Add("Jane", "69, Lance Drive");
// var diff = d1.Except(d2);
Run Code Online (Sandbox Code Playgroud)
假设我想获得上述两个词典的差异 var diff = d1.Except(d2);
现在我想对N字典的数量使用 lambda 表达式得到同样的结果。
一瞬间,我将两本词典合二为一。我想使用lambda表达式或任何其他LINQ表达式来获取两个字典的差异。
Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>();
d.Add("Test", d1);
d.Add("Test2", d2);
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的表达式,但没有得到任何结果。
d.Select(c => c.Value.Except(c.Value))
Run Code Online (Sandbox Code Playgroud) 我有一个FileInfo对象数组,其中包含我想要过滤的重复元素,即删除重复项,元素按上次写入时间使用自定义比较器排序.文件名的格式如下:
file {number} {YYYMMDD} {HHMMSS} .txt
我想知道的是,是否有一种优雅的方法可以过滤掉具有相同文件编号的两个文件,以便列表中只有最新的文件,即我的数组中有两个元素,文件名如下:
file1_20110214_090020.txt
file1_20101214_090020.txt
我想保留最新版本的file1.我获取文件的代码如下:
FileInfo[] listOfFiles = diSearch.GetFiles(fileSearch);
IComparer compare = new FileComparer(FileComparer.CompareBy.LastWriteTime);
Array.Sort(listOfFiles, compare);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
更新:
忘了添加警告,有问题的程序使用.Net 2.0,所以不幸的是没有LINQ.对不起,上面我纠正了文件编号是一样的
我需要一些帮助,使用 lambda 表达式删除实体框架上下文中的重复条目。我有一个包含以下列的表格:
Id, DateOfIncident, Description, EmployeeId, IncidentTypeId, and IsAttendanceIncident
我想删除DateOfIncident, EmployeeID, IncidentTypeID and IsAttendanceIncident are the same. 我想保留一个条目的重复条目。我知道如何在 SQL 中使用带有 CTE 的存储过程来执行此操作,但我无法弄清楚如何使用 Lambda 表达式完成此任务。
此代码返回一个不包括我的重复项的列表,但现在我如何删除不在此列表中的那些重复项?
var query = db.Incidents.Where(x => x.IsAttendanceIncident == "Y").GroupBy(x => new { x.EmployeeId, x.DateOfIncident, x.IsAttendanceIncident })
.Select(x => x.FirstOrDefault());
Run Code Online (Sandbox Code Playgroud)
更新:
所以我继续编写自定义 IEqualityComparer。现在如何使用 id 删除我的上下文中不在我的 distinctItems 中的事件?
static void Main(string[] args)
{
DALIncidents.AttendanceEntities1 db = new DALIncidents.AttendanceEntities1();
IEnumerable<DALIncidents.Incident> distinctItems = db.Incidents.Where(c => c.IsAttendanceIncident == "Y");
distinctItems = distinctItems.Distinct(new DALIncidents.DistinctIncidentComparer());
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud) public Material
{
public MaterialType MaterialKind {get;set;}
}
enum MaterialType
{
Iron,
Plastic,
Carbon,
Wood
}
Run Code Online (Sandbox Code Playgroud)
我有一个材料项目列表.
var items = new List<Material>
{
new Material{ MaterialKind = MaterialType.Iron},
new Material{ MaterialKind = MaterialType.Plastic},
new Material{ MaterialKind = MaterialType.Carbon},
new Material{ MaterialKind = MaterialType.Iron},
new Material{ MaterialKind = MaterialType.Wood},
}
Run Code Online (Sandbox Code Playgroud)
每种类型在材料清单中应该只有一次可用的最大值.
我怎么写这个:
bool isItemsListDistinct =材料.??
上述样本至少有一个副本,即"Iron",因此返回值必须为false.
从C#中的List中删除重复项
我有一个数据读取器从数据库中读取字符串.
我使用List来聚合从数据库读取的字符串,但是我在这个字符串中有重复项.
任何人都有一个快速方法从C#中的通用列表中删除重复项?
List<string> colorList = new List<string>();
public class Lists
{
static void Main()
{
List<string> colorList = new List<string>();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
colorList.Add(variableFromDB.ToString());
foreach (string color in colorList)
{
Response.Write(color.ToString().Trim());
}
}
}
Run Code Online (Sandbox Code Playgroud) 我是统一的新手并使用C#,实际上我是python开发人员我试图制作一个只能包含唯一值的列表,如果有一些重复值,它将不允许进入列表
List<int> iList = new List<int>();
iList.Add(2);
iList.Add(3);
iList.Add(5);
iList.Add(7);
Run Code Online (Sandbox Code Playgroud)
list = [2,3,5,7]
**在python中我们这样做是为了避免重复列表**
if(iList.indexof(value)!=-1){
iList.append(value)
}
Run Code Online (Sandbox Code Playgroud)
但是我们应该如何在C#中实现非常相似的结果谢谢您的努力将受到高度赞赏
c# ×10
linq ×5
.net ×3
duplicates ×2
lambda ×2
arrays ×1
asp.net ×1
c#-3.0 ×1
c#-4.0 ×1
dictionary ×1
gethashcode ×1
list ×1
sql ×1
sql-server ×1
unity5 ×1