如何获取列表元素的值? [英] How can I get the the value of an element of a list?

查看:309
本文介绍了如何获取列表元素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//How to get the price of a certain product from the list? Example the Ipad. How can I get the price and return the value in a label.


//My Class

public class ProductInfo

    {
       string _name;
       string _location;
       double _price;

    public ProductInfo(string name, string location, double price)
    {
       _name = name;
       _location = location;
       _price = price;
    }


 public string Name{ get; set}

 public string Location{ get; set}

 public double Price{ get; set}

}
}
//This is my List
    List<ProductInfo> Product = new List< ProductInfo >();

            Product.Add(new ProductInfo ("Ipad", "USA", 585.56));
            Product.Add(new ProductInfo ("Laptop", "USA", 1227.75));
            Product.Add(new ProductInfo ("MemoryCard", "USA", 32.77));
            Product.Add(new ProductInfo ("Mouse", "USA", 56.19));
            Product.Add(new ProductInfo ("HardDisk", "USA", 165.50));
            Product.Add(new ProductInfo ("Monitor", "USA", 286.90,));

推荐答案

这里是一个更干净的解决方案:
Here is a cleaner solution:
var price = Products.FirstOrDefault(i => i.Name = productName).Select(i => i.price);



在良好的设计中,不应该出现返回空值的情况,如果这样做,则可能是一个例外.这可能是一个例外,因为代码中的某个地方存在错误.上面的代码可以使用try catch块捕获异常.但是,如果要使用默认值,请正确使用Linq:



In good design, there should not be a case where you get a null back, and if you do, it should probably be an exception. This is probably an exception because there is a bug somewhere in the code. An exception can be caught with the above code with a try catch block. But if you want a default then use Linq properly:

var price = Products.FirstOrDefault(i => i.Name = productName).Select(i => i i == null ? 0 : i.price);


IEnumerable 接口的FirstOrDefault extension方法将返回第一次出现或默认值集合成员.
The FirstOrDefault extension method of IEnumerable interface returns the first occurance or the default value of the collection member.
var price = Products.FirstOrDefault(i => i.Name = productName).Select(i => i.price);


在这种情况下,它返回ProductInfo 对象.
ProductInfo 类没有Select 方法,因此,.Select 将在上面的语句中引发错误.
因此,我们必须使用.Price代替.Select(i => i.price).
ProductName 是一个字符串,在i.Name中= = productName为==用于比较

但是,当List 为空时,FirstOrDefault 返回null ,因为引用类型的默认值为null,并且如果找不到所需的乘积,则说我们通过了Ipad2,则该方法返回null.无论哪种情况,如果我们直接访问FindOrDefault 方法返回的对象的Price属性,都有可能出错.因此,我认为更好的选择是


In this case it returns the ProductInfo object.
The ProductInfo class does not have Select method Hence, the .Select will throw error in the above statement.
So, instead of .Select(i => i.price) we have to use .Price.
ProductName is a string, = in i.Name = productName is to be == for comparison

But, when the List is empty the FirstOrDefault returns null as the default value for a reference type is null and if the required product is not found say we passed Ipad2, the method returns null. In either case if we directly access Price property on the object returned by the FindOrDefault method, there is a chance of error. Hence, I think the better option would be

ProductInfo selectedProductInfo = Products.FirstOrDefault(p => p.Name == productName);
double price = selectedProductInfo == null ? 0 : selectedProductInfo.Price;



克利福德·纳尔逊(Clifford Nelson)插入了以下文本
可能没有null问题,并且可以涵盖例外情况.在调试过程中可以放置一个Debug.Asser.也可以执行以下操作

var price =(Products.FirstOrDefault(i => i.Name = productName)?? new Preduct()).Select(i => i.price);



The following text inserted by Clifford Nelson
May not have an issue with null, and can cover with an exception. During debug can put an Debug.Asser. Can also do the following

var price = (Products.FirstOrDefault(i => i.Name = productName) ?? new Preduct()).Select(i => i.price);


Hello

第一件事.

您的班级必须这样定义:
Hello

First thing.

Your class must defined like this:
public class ProductInfo
{
    //string _name;
    //string _location;
    //double _price;

    public ProductInfo(string name, string location, double price)
    {
        Name = name;
        Location = location;
        Price = price;
    }


    public string Name { get; set; }

    public string Location { get; set; }

    public double Price { get; set; }
}



如果您确实想要完整的属性,那么:



If you realy want full properties, then:

public class ProductInfo
{
    public ProductInfo(string name, string location, double price)
    {
        Name = name;
        Location = location;
        Price = price;
    }

    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _location;
    public string Location
    {
        get { return _location; }
        set { _location = value; }
    }

    private double _price;
    public double Price
    {
        get { return _price; }
        set { _price = value; }
    }
}




然后获取价格:




Then for getting the price:

var prices = Product.Where(p => p.Name == "Mouse").Select(p => p.Price);
double price;
if (prices.Count() > 0)
    price = prices.First();


这篇关于如何获取列表元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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