将一个列表对象中的项与另一个列表对象中的另一个项进行比较

时间:2018-03-09 15:55:52

标签: c# .net linq

在下面的示例中,有两个产品类对象。在每个产品对象中包含供应商列表。每个对象可以有一个供应商作为收藏。如果来自两个对象的isFavorite供应商的id不同于我必须维护登录日志文件。但我得到运行时错误。 您的帮助可能会受到赞赏。

c#c​​ode

namespace Article_Example
{
    class product
    {
        private int _productId;
        private string _productName;
        private string _productDesc;
        private List<Suppliers> _suppList;
        public product(int productId, string productName, string productDesc, List<Suppliers> lst)
        {
            _productId = productId;
            _productName = productName;
            _productDesc = productDesc;
            _suppList = new List<Suppliers>(lst);
        }
        public int productId { get { return _productId; } set { _productId = value; } }
        public string productName { get { return _productName; }set { _productName = value; } }
        public string productDesc { get { return _productDesc; } set { _productDesc = value; } }
        public List<Suppliers> suppList { get { return _suppList; } set { _suppList = value; } }

    }
    class Suppliers
    {
        private int _supplierId;
        private string _supplierName;
        private bool _isFavourate;
        public Suppliers(int supplierId, string supplierName, bool isFavourate)
        {
            _supplierId = supplierId;
            _supplierName = supplierName;
            _isFavourate = isFavourate;
        }
        public int suppliersId { get { return _supplierId; } set { _supplierId = value; } }
        public string suppliersName { get { return _supplierName; } set {_supplierName = value; } }
        public bool isFavourate { get { return _isFavourate; } set { _isFavourate = value; } }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Suppliers supp1 = new Suppliers(supplierId: 1, supplierName: "Supp1", isFavourate: true);
            Suppliers supp2 = new Suppliers(supplierId: 2, supplierName: "Supp2", isFavourate: false);
            Suppliers supp3 = new Suppliers(supplierId: 3, supplierName: "Supp3", isFavourate: false);
            Suppliers supp4 = new Suppliers(supplierId: 4, supplierName: "Supp4", isFavourate: false);

            List<Suppliers> lst1 = new List<Suppliers>();
            lst1.Add(supp1);
            lst1.Add(supp2);
            lst1.Add(supp3);
            lst1.Add(supp4);

            product product1 = new product(1, "Product 1", "Product Desc 1 ", lst1);


            Suppliers supp21 = new Suppliers(supplierId: 1, supplierName: "Supp1", isFavourate: false);
            Suppliers supp22 = new Suppliers(supplierId: 2, supplierName: "Supp2", isFavourate: false);
            Suppliers supp23 = new Suppliers(supplierId: 3, supplierName: "Supp3", isFavourate: true);
            Suppliers supp24 = new Suppliers(supplierId: 4, supplierName: "Supp4", isFavourate: false);

            List<Suppliers> lst2 = new List<Suppliers>();
            lst1.Add(supp21);
            lst1.Add(supp22);
            lst1.Add(supp23);
            lst1.Add(supp24);

            product product2 = new product(1, "Product 2", "Product Desc 2 ", lst2);

            int s1 = Convert.ToInt16(product1.suppList.Where(x => x.isFavourate == true));
            int s2 = Convert.ToInt16(product2.suppList.Where(x => x.isFavourate == true));
            if(!(s1.Equals(s2)))
            {
                string log = "Supplier " + s1 + " has changed to " + s2 + " !";
            }
        }
    }
}

以下是错误:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type
'WhereListIterator`1[Article_Example.Suppliers]' to type 'System.IConvertible'.
   at System.Convert.ToInt16(Object value)
   at Article_Example.Program.Main(String[] args) in c:\users\admin\documents\vi
sual studio 2015\Projects\Article Example\Article Example\Program.cs:line 74
Press any key to continue . . .                                         

谢谢, Nandkumar Satpute

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,表达式product1.suppList.Where(x => x.isFavourate == true)的返回值似乎不是整数类型。这导致InvalidCast异常。请尝试以下表达式

int s1 = Convert.ToInt16(product1.suppList.FirstOrDefault(x => x.isFavourate == true).supplierId); 

将修复无效的投射例外