Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C在字符串数组列表中查找元素_C# - Fatal编程技术网

C# C在字符串数组列表中查找元素

C# C在字符串数组列表中查找元素,c#,C#,我现在几个小时都不能解决一个问题。 下面是一个简化的场景。 比如说,有一个有出价的人的名单。我正试图找到一个出价最高的人,并返回他的名字。我能够找到最高出价,但如何输出名称 List<String[]> list = new List<String[]>(); String[] Bob = { "Alice", "19.15" }; String[] Alice = {"Bob", "28.20"}; Str

我现在几个小时都不能解决一个问题。 下面是一个简化的场景。 比如说,有一个有出价的人的名单。我正试图找到一个出价最高的人,并返回他的名字。我能够找到最高出价,但如何输出名称

        List<String[]> list = new List<String[]>();
        String[] Bob = { "Alice", "19.15" };
        String[] Alice = {"Bob", "28.20"};
        String[] Michael = { "Michael", "25.12" };

        list.Add(Bob);
        list.Add(Alice);
        list.Add(Michael);

        String result = list.Max(s => Double.Parse(s.ElementAt(1))).ToString();

        System.Console.WriteLine(result);
结果得到28.20,这是正确的,但我需要显示Bob。有太多与list.Select的组合,但没有成功。有人吗?

你可以用

有关用法,请参见 e、 g.在这种情况下

list.MaxBy(s => Double.Parse(s.ElementAt(1)))[0]

更多

从架构的角度来看,最好的解决方案是创建一个单独的类,例如包含两个属性的Person,每个人的姓名和出价,以及包含人员列表的类Person

然后您可以轻松地使用LINQ命令

另外,不要将出价存储为字符串,而是考虑是否将出价存储为浮点值或十进制值更好,或者将其存储为美分并使用int

我手边没有编译器,所以我有点想不通:

public class Person
{
    public string Name { get; set; }
    public float  Bid  { get; set; }

    public Person(string name, float bid)
    {
        Debug.AssertTrue(bid > 0.0);
        Name = name;
        Bid = bid;
    }
}

public class Persons : List<Person>
{
    public void Fill()
    {
        Add(new Person("Bob", 19.15));
        Add(new Person("Alice" , 28.20));
        Add(new Person("Michael", 25.12));
    }
}

找到结果后,只需执行以下操作:

list.First(x=>x[1] == result)[0]

这在这个简单的例子中是有效的。不确定是不是真的

var result = list.OrderByDescending(s => Double.Parse(s.ElementAt(1))).First();
应:

var max = list.Max(t => double.Parse(t[1]));
list.First(s => double.Parse(s[1]) == max)[0]; // If list is not empty

与其使用字典,不如使用类来实现这一点。查看Michel Keijzers的答案非常感谢,这就是我一直在寻找的答案!也许没有定义一个类那么优雅,但我真的试图避免它。干杯我同意,这门课比较容易对付。但由于问题的性质,web服务等,我不想创建额外的类。谢谢你抽出时间!你在那里使用的Max方法不返回int吗?没问题。。。无论如何,我扩展了答案,以向其他人展示类如何帮助提高可读性和分担责任。我将Max改为MaxBy,假设它返回出价最高的人,然后取那个人的名字。是吗?我不记得.NET框架中有MaxBy。
var max = list.Max(t => double.Parse(t[1]));
list.First(s => double.Parse(s[1]) == max)[0]; // If list is not empty