iub3rskillz iub3rskillz - 3 months ago 18
C# Question

C# Value from another List linked to value

I am trying to get the productname from the List where it matches the Productid from the List using LINQ but I have no luck.

The ProductID is in both lists and It should be linked somehow.

I tried:

Product product = products.FirstOrDefault(x => x.ProductName == orderDetail.ProductID);


List<orderDetail> orderDetails = Database.getOrderDetails();
List<Product> products = Database.getProducts();

textBox1.Text = Convert.ToString(orderDetail.OrderID);
textBox2.Text = Convert.ToString(Productname);
textBox3.Text = Convert.ToString(orderDetail.UnitPrice);
textBox4.Text = Convert.ToString(orderDetail.Quantity);
textBox5.Text = Convert.ToString(orderDetail.Discount);

Answer

I think you want something like this, but it's not very clear how your Product and Order objects are structured. (You'll also probably want some null checks.)

OrderDetails orderDetails = Database.GetOrderDetails().First();
List<Product> products = Database.GetProducts();

var name = products.First(p => p.ProductID == orderDetails.ProductID).ProductName;
Comments