Linq list with counts
Return a list with item counts such as
Apples - 3
Oranges -7
Berries - 15
Apples - 3
Oranges -7
Berries - 15
public List<SelectListItem> MyFruityList()
{
//Declare a new list of items
List<SelectListItem> items = new List<SelectListItem>();
int totalcount = 0;
//get all your objects and add to a variable
var allfruits= prog.getallFruits();
//This next method returns a FruitId, FruitName and FruitCount
var pg = prog.getExpiringFruitCounts().OrderByDescending(p => p.FruitCount);
var alljoined = (from a in allfruits
join c in pg
on new { a.FruitId, a.FruitName } equals new { c.FruitId, c.FruitName } into joinedData
from c in joinedData.DefaultIfEmpty() group a by new { a,c } into ord
select new SpecialFruit_DTO
{
Fruitid = ord.Key.a.FruitId,
FruitName =ord.Key.a.FruitName ,
OnHandCount= ord.Key.c.FruitCount
} ).ToList();
foreach (var p in alljoined)
{
int count= p.OnHandCount== null ? 0 : p.OnHandCount;
items.Add(new SelectListItem { Text = p.FruitName + " [" + count + "]", Value =p.FruitId.ToString() });
}
}
{
//Declare a new list of items
List<SelectListItem> items = new List<SelectListItem>();
int totalcount = 0;
//get all your objects and add to a variable
var allfruits= prog.getallFruits();
//This next method returns a FruitId, FruitName and FruitCount
var pg = prog.getExpiringFruitCounts().OrderByDescending(p => p.FruitCount);
var alljoined = (from a in allfruits
join c in pg
on new { a.FruitId, a.FruitName } equals new { c.FruitId, c.FruitName } into joinedData
from c in joinedData.DefaultIfEmpty() group a by new { a,c } into ord
select new SpecialFruit_DTO
{
Fruitid = ord.Key.a.FruitId,
FruitName =ord.Key.a.FruitName ,
OnHandCount= ord.Key.c.FruitCount
} ).ToList();
foreach (var p in alljoined)
{
int count= p.OnHandCount== null ? 0 : p.OnHandCount;
items.Add(new SelectListItem { Text = p.FruitName + " [" + count + "]", Value =p.FruitId.ToString() });
}
}
Comments