通用帮助最小化 [英] Generic help minimum

查看:99
本文介绍了通用帮助最小化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

client.add(new client ( name ,adress,number))



我有大约100个我想得到的最小值数字(数字)

并获得该指数列表中的数字


I have about 100 of these i want to get the minimum value of number(number)
And get the index of that number in the list

推荐答案

最简单的方法就是使用循环:

Easiest way is just to use a loop:
int minIndex = 0;
for(int index = 0; index < clients.Count; index++)
   {
   if (clients[index].number < clients[minIndex].number)
      {
      minIndex = index;
      }
   }

你可以使用Linq Min方法,但因为它是一个自定义类,所以设置起来要复杂得多(而且调试起来要困难得多)

You can to it with the Linq Min method, but as it's a custom class it's quite a bit more complex to set up (and a lot harder to debug)


// required
using System.Linq;

var data = new dynamic[] {
    new { a = "a", n = 10 }, 
    new { a = "b", n = 5 }, 
    new { a = "c", n = 6 },
    new { a = "d", n = 2 } 
}.ToList();

int min = data.Min(x => x.n);

int ndx = data.IndexOf(data.First(i => i.n == min));

Console.Write("minimum: {0} index of minimum: {1}", min, ndx);

在这种情况下,需要将匿名类型的动态数组转换为List才能使用'IndexOf:取决于您的集合的创建方式,您可能不会需要这样的转换。



另外,请注意这里假设使用Enumerable .Min不会抛出错误,并且您总是希望类集合中的第一个实例其中'n的值等于计算的最小值。

In this case converting the dynamic array of anonymous types to a List was required in order to use 'IndexOf: depending on how your collection is created, you may not need such conversion.

Also, note the assumption here that using Enumerable .Min will not throw an error, and that you always want the first instance in the collection of classes where the value of 'n is equal to the computed minimum.


请通过 BillWoodruff [ ^ ]。如果客户列表可以存储多个带有最小数字字段的项目,则需要列出所有满足标准的客户。



如果你想获得所有具有最小数字属性的项目,你需要以这种方式编写查询(例如):

Please, read comments to the solution 2 by BillWoodruff[^]. In case when the list of clients can store more then one item with the minimal number field, you need to list all clients fulfilling the criteria.

If you want to get all items with minimal number property, you need to write query this way (for example):
void Main()
{
    List<client> clients = new List<client>{new client("A","B", 10),
    new client("C","D", 12),new client("E","F", 3),
    new client("G","H", 9),new client("I","J", 5),
    new client("K","L", 3),new client("M","N", 7),
    new client("O","P", 8),new client("Q","R", 11),
    new client("S","T", 10),new client("U","V", 19),
    new client("X","Y", 3),new client("Z","Z", 12)};

    var qry = clients.Select((c, i) => new{client = c, Index=i}).
        Where(x => x.client.Number==(from cl in clients select cl.Number).Min()).
        Select(x=>x.Index).ToList();

    foreach(int item in qry)
    {
        Console.WriteLine("{0}", item.ToString());
    }

}

// Define other methods and classes here
class client
{
    private string sname = string.Empty;
    private string saddress = string.Empty;
    private int inumber = 0;

    public client(string aName, string aAddress, int aNumber)
    {
        sname = aName;
        saddress = aAddress;
        inumber = aNumber;
    }

    public string Name
    {
        get{return sname;}
        set{sname=value;}
    }

    public string Address
    {
        get{return saddress;}
        set{saddress=value;}
    }

    public int Number
    {
        get{return inumber;}
        set{inumber=value;}
    }

}





退货:



Returns:

2
5
11



对应客户名称: E K X


这篇关于通用帮助最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆