Optimize query with LinQ, how?

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello,

I have a list of orders with material.

If all three slots are used, the function must return false.

My function works well, but I think there is a better and easier way.

How could this look like?


if (ColOrders == null || ColOrders.Count == 0)
return true;

bool slot1, slot2, slot3;
slot1 = slot2 = slot3 = false;
foreach (var item in ColOrders)
{
slot1 = slot1 == false ? (item.Materials.FirstOrDefault(y => y.SlotNo == 1) != null ? true : false) : slot1;
slot2 = slot2 == false ? (item.Materials.FirstOrDefault(y => y.SlotNo == 2) != null ? true : false) : slot2;
slot3 = slot3 == false ? (item.Materials.FirstOrDefault(y => y.SlotNo == 3) != null ? true : false) : slot3;

if (slot1 && slot2 & slot3)
{
return false;
}
}


public class Order
{
public string OrderNumber { get; set; }
public string OrderName { get; set; }
public List<Material> Materials { get; } = new List<Material>();
}

public class Material
{

public string MaterialNumber { get; set; }
public int Quantity { get; set; }
public int SlotNo { get; set; }



Thanks in advance.

Greetings Markus

Continue reading...
 
Back
Top