2
我需要從嵌套對象中拉出特定值而不使用foreach
循環。我認爲這裏正確的方法是一個linq查詢,但我無法獲得我需要的值。考慮到類結構:Linq查詢從嵌套對象獲取值
public class Order
{
public int OrderID { get; set; }
public List<OrderItems> { get; set; }
}
public class OrderItems
{
public int OrderItemID { get; set; }
public string ItemName { get; set; }
public int Quantity { get; set; }
public List<OrderItemShipping> OrderItemShippings { get; set; }
}
public class OrderItemShipping
{
public int OrderItemShippingID { get; set; }
public Address ShipAddress { get; set; }
public class Address
{
public int AddressID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}
我希望能夠做一些事情,如:
var shipToAddress = Order.OrderItems.OrderItemShipping.FirstOrDefault(x => x.Address.Address1);
但我的語法必須是不正確的,因爲我不能搶我需要的價值?
也許'SelectMany'是你在找什麼? – DavidG
其中是您的屬性名稱類型'列表'在訂單類? –
你需要在你的linq中有一些操作,比如x => x.Address.Address1 ==「something」 –