急求一个比较器写法

我2我骄傲 2017-02-20 06:38:24
public class Cell : IEquatable<Cell>
{
public bool Equals(Cell other)
{
if (other == null) return false;
if (this == other) return true;
return this.WhatDay == other.WhatDay && this.Lesson == other.Lesson;
}

/// <summary>
/// 星期几
/// </summary>
public int WhatDay { get; set; }

/// <summary>
/// 第几节课
/// </summary>
public int Lesson { get; set; }

}

public class WeekCell
{
public WeekCell()
{
CellList = new List<Cell>();
}

//周
public int Week
{
get;
set;
}

//节次
public List<Cell> CellList
{
get;
set;
}
}


上面是两个类

现在有一个List<WeekCell>
比如里面的数据是 (代码就随便写了,不要在乎语法)
week=1 List<Cell> = {whatday=1,lesson=1},{whatday=1,lesson=2}
week=2 List<Cell> = {whatday=1,lesson=1},{whatday=1,lesson=2}
week=1 List<Cell> = {whatday=1,lesson=2,{whatday=1,lesson=3}

我现在想得到的结果是List<WeekCell> 里面的数据是
week=1 List<Cell> = {whatday=1,lesson=1},{whatday=1,lesson=2},{whatday=1,lesson=3}
week=2 List<Cell> = {whatday=1,lesson=1},{whatday=1,lesson=2}

...全文
193 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
我2我骄傲 2017-02-21
  • 打赏
  • 举报
回复
谢谢!LINQ 简单的还可以 ,复杂的 就不是很熟悉了。。。。
Poopaye 2017-02-20
  • 打赏
  • 举报
回复
listWeekCell
	.GroupBy(weekCell => weekCell.Week)
	.Select(item => new WeekCell
	{
		Week = item.Key,
		CellList = item.SelectMany(weekCell => weekCell.CellList).Distinct().ToList()
	});
Cell还要重写GetHashCode

public class Cell : IEquatable<Cell>
{
	//...

	public override int GetHashCode()
	{
		return this.WhatDay * 33 + this.Lesson;
	}
}
我2我骄傲 2017-02-20
  • 打赏
  • 举报
回复
不要沉下去,。。。。。。
我2我骄傲 2017-02-20
  • 打赏
  • 举报
回复
shingoscar 麻烦帮忙看看,另外不一定是比较器

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧