我有一系列公司及其数据(地址,电子邮件,电话,活动?(无论它们是否在我们的系统中有效))。我可以轻松地对名称上的列表进行排序,但我需要一些不同的东西。该列表有重复项,我想先按名称排序,然后按活动或非活动排序。然后,我想要标记不活动的重复项,以便我可以删除它们。
现有代码跨多个类,但这里是jist(如果excel单元格为null,则fixNullValues()只返回一个空字符串):
for (int i = 2; i <= xlRange.Rows.Count; i++)
{
firm = new Firm();
string begCell;
begCell = "B" + i;
excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
firm.acctNo = fixNullValues(Convert.ToString(excelCell.Value2));
begCell = "C" + i;
excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
firm.name = fixNullValues(Convert.ToString(excelCell.Value2));
begCell = "D" + i;
excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
firm.addy1 = fixNullValues(Convert.ToString(excelCell.Value2));
begCell = "E" + i;
excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
firm.city = fixNullValues(Convert.ToString(excelCell.Value2));
begCell = "AB" + i;
excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
if (Convert.ToString(excelCell.Value2).Equals("Active", StringComparison.OrdinalIgnoreCase))
firm.active = true;
firmList.Add(firm);
答案 0 :(得分:0)
见下面的代码:
class Firm : IComparable<Firm>, IComparable
{
string acctNo;
string name;
string addy1;
string city;
bool active;
int completeness
{
get
{
return accNo.Length + name.Length + addy1.Length + city.Length;
}
}
public int CompareTo(Firm other)
{
var c = name.CompareTo(other.name);
if (c != 0)
return c;
c = active.CompareTo(other.active);
if (c != 0)
return c;
return completeness.CompareTo(other.completeness);
}
public int CompareTo(object other)
{
return CompareTo((Firm)other);
}
public static IEnumerable<Firm> sortFirms(IEnumerable<Firm> firms)
{
return firms.GroupBy(f => f.name).Select(g => g.OrderByDescending().First());
}
}
答案 1 :(得分:0)
var yourNewListWithoutDeleting = firmList.GroupBy(x => x.name)
.SelectMany(x =>
x.OrderByDescending(y => y.active)
.Take(1)
);