1
我的應用程序是.net核心MVC。我有兩個類asp.net c#linq從模型庫生成列表從另一個列表中的編號
public class Product
{
public int Id { get; set; }
public string Buyer { get; set; }
public int? ProductId { get; set; }
public ProductType ProductType { get; set; }
}
public class ProductType
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public ICollection<Product> Product { get; set; }
}
我想用下面生成的產品名稱的列表:
List<ProductType> productnames;
var products = _context.Product().Where(x => x.Buyer == "Test" && x.ProductId != null).ToList();
foreach (var item in products)
{
productnames = _context.ProductType.Where(c => c.ProductId == item.ProductId).ToList();
}
雖然我已經在列表(產品)3項,我得到的只是一個項目產品名稱列表。
注意:我無法使用Include,因爲我使用另一個Core Web API檢索數據,並且 無法使用oData返回IQueryable對象。所以作爲一種解決方法,我是 使用Sqlite作爲客戶端應用程序,而API使用MS Sql。
@StephenMuecke簡化這個,謝謝。它效果很好。 – hncl
所以用綠色的勾子檢查他的答案 – GGO