62,041
社区成员
发帖
与我相关
我的任务
分享
class priceobj
{
public string name { get; set; }
public int price { get; set; }
}
List<priceobj> prices = new List<priceobj>();
for (int a = 0; a < arr2.Length/arr2.Rank; a++)
{
bool isfind = false;
foreach (var item in prices)
{
if (item.name == arr2[a, 0])
{
isfind = true;
if (item.price > int.Parse(arr2[a, 1]) && int.Parse(arr2[a, 1]) != 0)
{
item.price = int.Parse(arr2[a, 1]);
}
}
}
if (!isfind)
prices.Add(new priceobj { name = arr2[a, 0], price = int.Parse(arr2[a, 1]) });
}
foreach (var item in prices)
{
Console.WriteLine(item.name+"|"+item.price.ToString());
}
Console.Read();
class Program
{
static void Main(string[] args)
{
Dictionary<string, decimal> dic = GetResult();
foreach (KeyValuePair<string, decimal> kvp in dic)
{
Console.WriteLine("name:" + kvp.Key + " price:" + kvp.Value);
}
}
public static Dictionary<string, decimal> GetResult()
{
string[] strNames = new string[] { "名称1", "名称2", "名称3" };
IList<a> list = new List<a>(){
new a("名称1",150),
new a("名称1",160),
new a("名称1",0),
new a("名称1",200),
new a("名称1",300),
new a("名称1",150),
new a("名称1",-50),
new a("名称2",150),
new a("名称2",0),
new a("名称2",300),
new a("名称2",250),
new a("名称2",-100),
new a("名称2",100)};
Dictionary<string, decimal> dic = list.Where(p => p.price > 0).
GroupBy(p => p.name).
Select(p => p.OrderBy(q => q.price).First()).ToDictionary(p => p.name, q => q.price);
foreach (string s in strNames)
{
if (!dic.ContainsKey(s))
{
dic.Add(s, 0);
}
}
return dic;
}
}
public class a
{
public string name { get; set; }
public decimal price { get; set; }
public a(string name, decimal price)
{
this.name = name;
this.price = price;
}
}
//名称1 150
//名称1 160
//名称1 0
//名称1 200
//名称1 300
//名称2 150
//名称2 0
//名称2 150
//名称2 300
//名称2 250
//名称2 100
//声明名称1 和2的数组
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
//添加名称1的 price
list1.Add(150);
list1.Add(160);
list1.Add(200);
list1.Add(150);
//list1.Min().ToString() 取最小值
Response.Write(list1.Min().ToString());
//名称2的同理
List<Money> list = new List<Money>() { new Money("名称1", 150),new Money("名称1", 160),new Money("名称1", 0),new Money("名称1", 200),
new Money("名称2", 0),new Money("名称2", 100) , new Money("名称2", 80)};
var result = from c in list where c.Price > 0 group c by c.Name into g select new { g.Key, price = g.Min(c => c.Price) };
foreach (var money in result)
{
Response.Write(money.Key + "=" + money.price + "<br/>");
}