一组数据 list集合,怎么才能变成我想得到的结果

wzy528 2012-09-20 07:15:06
例如有这么一组数据


小明,小李,16,-7
小明,小李,16,-8
小明,小李,17,-7

小李,17,1
小李,17,2

小明,小李,小红,15,-10
小明,小李,小红,15,-9

小明,-10,18
小明,-9,18

怎么才能得到相同数据中最大的那一组数据,名字都是放在list<name>里面,只是比较这些数值,最后在这组数据中我想的到这样的效果:
小明,小李,17,-7
小李,17,2
小明,小李,小红,15,-9
小明,-9,18

-----------------------------------------------------------------------------------------------------------




public Form1()
{
InitializeComponent();


IList<TempNum> listTempNum = new List<TempNum>();


TempNum tempNum1 = new TempNum();
IList<string> listName1 = new List<string>();
listName1.Add("小明");
listName1.Add("小李");
tempNum1.Num1 = 16;
tempNum1.Num2 = -7;
tempNum1.Count = listName1.Count;

TempNum tempNum2 = new TempNum();
IList<string> listName2 = new List<string>();
listName2.Add("小明");
listName2.Add("小李");
tempNum2.Num1 = 16;
tempNum2.Num2 = -8;
tempNum2.Count = listName2.Count;

TempNum tempNum3 = new TempNum();
IList<string> listName3 = new List<string>();
listName3.Add("小明");
listName3.Add("小李");
tempNum3.Num1 = 17;
tempNum3.Num2 = -7;
tempNum3.Count = listName3.Count;


TempNum tempNum4 = new TempNum();
IList<string> listName4 = new List<string>();
listName4.Add("小李");
tempNum4.Num1 = 17;
tempNum4.Num2 = 1;
tempNum4.Count = listName4.Count;


TempNum tempNum5 = new TempNum();
IList<string> listName5 = new List<string>();
listName5.Add("小李");
tempNum5.Num1 = 17;
tempNum5.Num2 = 2;
tempNum5.Count = listName5.Count;



TempNum tempNum6 = new TempNum();
IList<string> listName6 = new List<string>();
listName6.Add("小明");
listName6.Add("小李");
listName6.Add("小红");
tempNum6.Num1 = 15;
tempNum6.Num2 = -10;
tempNum6.Count = listName6.Count;

TempNum tempNum7 = new TempNum();
IList<string> listName7 = new List<string>();
listName7.Add("小明");
listName7.Add("小李");
listName7.Add("小红");
tempNum7.Num1 = 15;
tempNum7.Num2 = -9;
tempNum7.Count = listName7.Count;

TempNum tempNum8 = new TempNum();
IList<string> listName8 = new List<string>();
listName8.Add("小明");
tempNum8.Num1 = -10;
tempNum8.Num2 = 18;
tempNum8.Count = listName8.Count;

TempNum tempNum9 = new TempNum();
IList<string> listName9 = new List<string>();
listName9.Add("小明");
tempNum9.Num1 = -9;
tempNum9.Num2 = 18;
tempNum9.Count = listName9.Count;






listTempNum.Add(tempNum1);
listTempNum.Add(tempNum2);
listTempNum.Add(tempNum3);
listTempNum.Add(tempNum4);
listTempNum.Add(tempNum5);
listTempNum.Add(tempNum6);
listTempNum.Add(tempNum7);
listTempNum.Add(tempNum8);
listTempNum.Add(tempNum9);

dataGridView1.DataSource = listTempNum;

}

public class TempNum
{
private int num1;

public int Num1
{
get { return num1; }
set { num1 = value; }
}
private int num2;

public int Num2
{
get { return num2; }
set { num2 = value; }
}
private IList<string> listString = new List<string>();

public IList<string> ListString
{
get { return listString; }
set { listString = value; }
}

private int count;

public int Count
{
get { return count; }
set { count = value; }
}

}






显示的是这样的






最后大概是这个样子吧,只要得到每组相同的list<name>里面数值最大的那组




我临时举了个列子,如果有上千条这样的数据,肯定要用循环,不知道怎么循环,大家帮帮我,谢谢,我只有27分,都给了。
...全文
219 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2012-09-20
  • 打赏
  • 举报
回复
楼主的这种提问方式很好

给出的类定义代码和测试数据 赞一个!
q107770540 2012-09-20
  • 打赏
  • 举报
回复

void Main()
{
IList<TempNum> listTempNum = new List<TempNum>();


TempNum tempNum1 = new TempNum();
IList<string> listName1 = new List<string>();
listName1.Add("小明");
listName1.Add("小李");
tempNum1.Num1 = 16;
tempNum1.Num2 = -7;
tempNum1.ListString=listName1; //注意此处
tempNum1.Count = listName1.Count;

TempNum tempNum2 = new TempNum();
IList<string> listName2 = new List<string>();
listName2.Add("小明");
listName2.Add("小李");
tempNum2.Num1 = 16;
tempNum2.Num2 = -8;
tempNum2.ListString=listName2;
tempNum2.Count = listName2.Count;

TempNum tempNum3 = new TempNum();
IList<string> listName3 = new List<string>();
listName3.Add("小明");
listName3.Add("小李");
tempNum3.Num1 = 17;
tempNum3.Num2 = -7;
tempNum3.ListString=listName3;
tempNum3.Count = listName3.Count;


TempNum tempNum4 = new TempNum();
IList<string> listName4 = new List<string>();
listName4.Add("小李");
tempNum4.Num1 = 17;
tempNum4.Num2 = 1;
tempNum4.ListString=listName4;
tempNum4.Count = listName4.Count;


TempNum tempNum5 = new TempNum();
IList<string> listName5 = new List<string>();
listName5.Add("小李");
tempNum5.Num1 = 17;
tempNum5.Num2 = 2;
tempNum5.ListString=listName5;
tempNum5.Count = listName5.Count;



TempNum tempNum6 = new TempNum();
IList<string> listName6 = new List<string>();
listName6.Add("小明");
listName6.Add("小李");
listName6.Add("小红");
tempNum6.Num1 = 15;
tempNum6.Num2 = -10;
tempNum6.ListString=listName6;
tempNum6.Count = listName6.Count;

TempNum tempNum7 = new TempNum();
IList<string> listName7 = new List<string>();
listName7.Add("小明");
listName7.Add("小李");
listName7.Add("小红");
tempNum7.Num1 = 15;
tempNum7.Num2 = -9;
tempNum7.ListString=listName7;
tempNum7.Count = listName7.Count;

TempNum tempNum8 = new TempNum();
IList<string> listName8 = new List<string>();
listName8.Add("小明");
tempNum8.Num1 = -10;
tempNum8.Num2 = 18;
tempNum8.ListString=listName8;
tempNum8.Count = listName8.Count;

TempNum tempNum9 = new TempNum();
IList<string> listName9 = new List<string>();
listName9.Add("小明");
tempNum9.Num1 = -9;
tempNum9.Num2 = 18;
tempNum9.ListString=listName9;
tempNum9.Count = listName9.Count;

listTempNum.Add(tempNum1);
listTempNum.Add(tempNum2);
listTempNum.Add(tempNum3);
listTempNum.Add(tempNum4);
listTempNum.Add(tempNum5);
listTempNum.Add(tempNum6);
listTempNum.Add(tempNum7);
listTempNum.Add(tempNum8);
listTempNum.Add(tempNum9);

var query=listTempNum.GroupBy(x=>string.Join(",",x.ListString.ToArray())).Select(g=>g.OrderByDescending(x=>x.Num1+x.Num2).First());

query.ToList().ForEach(q=>Console.WriteLine("{0},{1},{2}",string.Join(",",q.ListString.ToArray()),q.Num1,q.Num2));

/*
小明,小李,17,-7
小李,17,2
小明,小李,小红,15,-9
小明,-9,18
*/
}
MuteG 2012-09-20
  • 打赏
  • 举报
回复

var result =
(from tempNum in listTempNum
group tempNum by string.Join(",", tempNum.ListString) into name
select (from groupNum in name
where groupNum.Num1 == name.Max((TempNum item) => item.Num1)
select groupNum).First()).ToList();
foreach (TempNum num in result)
{
Console.WriteLine("Num2={0}, Count={1}, ListString={2}, Num1={3}",
num.Num2, num.Count, string.Join(",", num.ListString), num.Num1);
}

我猜所有TempNum对象的ListString属性应该包含了人名对吧?
那么我给出的代码应该符合你的要求。

110,822

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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