I got a little issue with the lambda query.
I have 2 tables: Inventory and Articles. Also, I've created a list of parts I want to show.
public class SteelSheet
{
public string Part { get; set; }
public float Quantity { get; set; }
}
var sheetTest = _context.Inventories.Include(x => x.Article)......;
Articles include the field Part. I want to show data from Inventories and Articles which contain only Parts from steelSheet list.
edit:
public class Inventory
{
public int ID { get; set; }
public int Stock { get; set; }
public int ArticleID { get; set; }
public Article Article { get; set; }
}
public class Article
{
public int ID { get; set; }
public string Part { get; set; }
public float Price { get; set; }
public ICollection<Inventory> Inventory { get; set; }
}
steelSheet.Add(new SteelSheet() { Part = "PART0001", Quantity = "8" });
steelSheet.Add(new SteelSheet() { Part = "PART0002", Quantity = "3" });
SteelSheet is a list of parts which i want to show (i generete this list using another functions) But also i want to see price from Article and Stock from inventory.
CodePudding user response:
From you question there seems no need to base the query on Inventory
. The Article matches the Part number and each article contains a list of associated inventories.
The straightforward query would be
var parts = steelSheet.Select(s => s.Part).ToArray();
var matchingArticles = _context.Articles
.Where(a => parts.Contains(a.Part))
[.Select(a => [projection]]
[.ToList()/.ToArray()];
Things like .Include(a => a.Inventory)
are only required if you are using eager loading. read this.
CodePudding user response:
Articles include the field Part.
If you can get Article from your database , try to use below linq
, :
var sheetTest = _context.Article.Select(x=> new SteelSheet { Part=x.Part,Quantity=x.Quantity});