Sort list into children using linq - c#

I am trying to create a tree from a somewhat large list of 13883 objects using Linq to sort the objects into their parent and child groups, each object is associated with a parentId, I can group the objects together using
var sortedList = marketItems.GroupBy(p => p.parentId).Select(grp => grp.ToList()).ToList();
but this only sorts them into groups, I'm not yet fluent with linq and cant figure out how to map the children correctly. the null ParentGroup is the top level of the tree. Anyone with more experience with linq able to offer some ideas on how to group the llist correctly.
public JsonResult GetJsTree3Data()
{
var marketItems = new List<JsTree3Node>();
var itemList = new List<JsTree3Node>();
foreach (var group in GenerateGroups(connString))
{
var node = JsTree3Node.NewNode(group.id_str);
node.text = group.name;
node.state = new State(false, false, false);
node.parentId = group.marketParentGroup;
marketItems.Add(node);
}
foreach (var group in GenerateItems(connString))
{
var node = JsTree3Node.NewNode(group.id_str);
node.text = group.name;
node.state = new State(false, false, false);
node.parentId = group.marketParentGroup;
marketItems.Add(node);
}
// Create our root node and ensure it is opened
var root = new JsTree3Node()
{
id = "0",
text = "Market Items",
state = new State(true, false, false)
};
var sortedList = marketItems.GroupBy(u => u.parentId).Select(grp => grp.ToList()).ToList();
root.children = sortedList;
return Json(root, JsonRequestBehavior.AllowGet);
}
The final result I am trying achive is a tree of items that users can choose from. there is only one level of the tree as the children arent ordered in the sorted list

I see, the parents can contain multiple items: I believe this is what you are looking for:
var sortedList = from p in marketItems
group p by p.ParentGroup into Groups
select Groups;
//to access the items in each group individually
foreach (var grouping in sortedList)
{
foreach (var Item in grouping)
{
Item.Description///you can access the individual items from this layer...
}
}
Your code should do this, but to access the grouping items you need to use a nested foreach loop.

I'm not sure what your end goal is, but I'm not sure if what you're trying to do can be strictly done with a single LINQ pass. If it were possible, though, I imagine that it would be quite convoluted. You can do it in two LINQ queries quite simply, though:
var refDict = marketItems.ToDictionary(k => k.Id, v => v);
var groupedList = marketItems.Select(s =>
new
{
Parent = s.ParentGroup == null ? null : refDict[s.ParentGroup],
Child = s
}).ToList();
Then use it like this:
var parent = groupedList[0].Parent;
var child = groupedList[0].Child;
In all honesty, though, you could just keep yourself to the first line of code then query the dictionary for an object's parent yourself if you ever need it.

Related

SQL to LINQ expres

I'm trying to convert a SQL expression to Linq but I can't make it work, does anyone help?
SELECT
COUNT(descricaoFamiliaNovo) as quantidades
FROM VeiculoComSeminovo
group by descricaoFamiliaNovo
I try this:
ViewBag.familiasCount = db.VeiculoComSeminovo.GroupBy(a => a.descricaoFamiliaNovo).Count();
I need to know how many times each value repeats, but this way it shows me how many distinct values ​​there are in the column.
You can try:
var list = from a in db.VeiculoComSeminovo
group a by a.descricaoFamiliaNovo into g
select new ViewBag{
familiasCount=g.Count()
};
or
var list = db.VeiculoComSeminovo.GroupBy(a => a.descricaoFamiliaNovo)
.Select (g => new ViewBag
{
familiasCount=g.Count()
});
If you need column value:
new ViewBag{
FieldName=g.Key,
familiasCount=g.Count()
};
You don't need the GROUP BY unless there are fields other than the one in COUNT. Try
SELECT
COUNT(descricaoFamiliaNovo) as quantidades
FROM VeiculoComSeminovo
UPDATE, from your comment:
SELECT
COUNT(descricaoFamiliaNovo) as quantidades,
descricaoFamiliaNovo
FROM VeiculoComSeminovo
GROUP BY descricaoFamiliaNovo
That's it as SQL. In LINQ it is something like:
var reponse = db.VeiculoComSeminovo.GroupBy(a => a.descricaoFamiliaNovo)
.Select ( n => new
{Name = n.key,
Count = n.Count()
}
)
Not tested.
Ty all for the help.
I solved the problem using this lines:
// get the objects on db
var list = db.VeiculoComSeminovo.ToList();
// lists to recive data
List<int> totaisFamilia = new List<int>();
List<int> totaisFamiliaComSN = new List<int>();
// loop to cycle through objects and add the values ​​I need to their lists
foreach (var item in ViewBag.familias)
{
totaisFamilia.Add(list.Count(a => a.descricaoFamiliaNovo == item && a.valorSeminovo == null));
totaisFamiliaComSN.Add(list.Count(a => a.descricaoFamiliaNovo == item && a.valorSeminovo != null));
}
The query was a little slow than I expected, but I got the data

How to display LINQ Lambda Expression to a textbox

I am kind of new to Lambda Expressions, I have tried to work out a simple solution to the following task I have set myself.
A customer has a collection of cars. Use LINQ to get a total number of cars he has.
Code below, not sure if this is correct? My second question is how do you display the TotalNumberCars to a textbox?
using (Entities dbcontext = new Entities())
{
var ListByOwner = from c in dbcontext.Owners
where c.OwnerID == OwnerID
group c by c.Cars into g
select new
{
Owner = g.Key,
TotalNumberCars = g.Sum(x => x.Cars)
};
lblTotalCars.Text = ListByOwner.ToList();
}
I don't know how your entity data model is structured, but I would do it like this:
using (var dbContext = new Entities())
{
var numberOfCars = dbContext.Cars.Count(c => c.OwnerId == OwnderId);
lblTotalCars.Text = numberOfCars.ToString();
}
If there's no c.OwnerId then maybe you can access it by typing c.Owner.OwnerId.
ListByOwner.ToList() is an array (generic list) of your new items from your Select projection. Each item is a dynamic entity with two properties of Owner and TotalNumberCars. You need to index or foreach into the list, extract what is needed into a string and that can be your text.
Such as lblTotalCars.Text = ListByOwner[0].Owner; will display the first item's owner.
Take this and fill in what you need.
check this code
lblTotalCars.Text = ListByOwner.ToList().sum(c=>c.TotalNumberCars).ToString();

Compare two lists and add missing items by specific criteria

I have written a code like below:
foreach (var itemA in itm)
{
foreach (var itemB in filteredList)
{
if (itemA.ItemID != itemB.ItemID)
{
missingList.Add(itemB);
ListToUpdate.Add(itemB);
}
else
{
if (itemA.QuantitySold != itemB.QuantitySold)
{
ListToUpdate.Add(itemB);
}
}
}
}
So as you can see i have two lists here which are identical in their structure and they are:
List #1 is "itm" list - which contains old records from DB
List #2 is "filteredList" - which has all items from DB and + new ones
I'm trying to add items to missingList and ListToUpdate on next criteria:
All items that are "new" in filteredList - meaning their ItemID doens't exists in "itm" list should be added to missingList.
And all items that are new in filteredList- filteredList - meaning their ItemID doens't exists in "itm" list should be added to .ListToUpdate
And final criteria to add items to ListToUpdate should be those items that exist in both lists - and if the quantitysold in "itm" list is different - add them to ListToUpdate
The code above that I written gives me completely wrong results, I end up having more than 50000 items extra in both lists...
I'd like to change this code in a manner that it works like I wrote above and to possibly use parallel loops or PLINQ to speed things up...
Can someone help me out ?
Let's use Parallel.ForEach, which is available in C# 4.0:
Parallel.ForEach(filteredList, (f) =>
{
var conditionMatchCount = itm.AsParallel().Max(i =>
// One point if ID matches
((i.ItemID == f.ItemID) ? 1 : 0) +
// One point if ID and QuantitySold match
((i.ItemID == f.ItemID && i.QuantitySold == f.QuantitySold) ? 1 : 0)
);
// Item is missing
if (conditionMatchCount == 0)
{
listToUpdate.Add(f);
missingList.Add(f);
}
// Item quantity is different
else if (conditionMatchCount == 1)
{
listToUpdate.Add(f);
}
});
The above code uses two nested parallelised list iterators.
Following is an example to compare two lists which will give you list of new IDs.
Class I used to hold the data
public class ItemList
{
public int ID { get; set; }
}
Function to get new IDs
private static void GetNewIdList()
{
List<ItemList> lstItm = new List<ItemList>();
List<ItemList> lstFiltered = new List<ItemList>();
ItemList oItemList = new ItemList();
oItemList.ID = 1;
lstItm.Add(oItemList);
lstFiltered.Add(oItemList);
oItemList = new ItemList();
oItemList.ID = 2;
lstItm.Add(oItemList);
lstFiltered.Add(oItemList);
oItemList = new ItemList();
oItemList.ID = 3;
lstFiltered.Add(oItemList);
var lstListToUpdate = lstFiltered.Except(lstItm);
Console.WriteLine(lstListToUpdate);
}
For getting the list of common IDs use following
var CommonList = from p in lstItm
join q in lstFiltered
on p.ID equals q.ID
select p;
UPDATE 2
For getting the list of new IDs from filtered list based on ID
var lstListToUpdate2 = lstFiltered.Where(a => !lstItm.Select(b => b.ID).Contains(a.ID));

Using LINQ to group together a list of nodes and a list of their attributes

I have a table with a list of Nodes (with properties ID and Name), and a second table with a list of Node Attributes (with a reference Parent to the corresponding Node ID). I usually query for the node, and then get its attributes, to populate an object
public class NodeWithAttributes
{
public Node Node = new Node();
public List<NodeAttribute> NodeAttributes = new List<NodeAttribute>();
}
However, when I want to query for a list of 'NodeWithAttributes' things get complicated. I'm trying to group the attributes together by Parent, and then assigning them to the object - something like :
var results = (from n in dbNodes.Nodes
join na in dbNodes.NodeAttributes on n.ID equals na.Parent
group na by na.Parent
into g
select new NodeWithAttributes
{
Node = n,
NodeAttributes = g
}
).ToList();
But I can only seem to return the group, and not an object based on it. What can I do to return a List of NodeWithAttributes?
I think in your case no need to group by data. If you want Parent Children try to use query similar to following.
var query = from c in north.Customers
orderby c.CustomerID
select new
{
customer = c,
orders = c.Orders
};
foreach (var item in query)
{
Console.WriteLine(item.customer.CompanyName);
foreach (var ord in item.orders)
{
Console.WriteLine(ord.OrderDate + " " + ord.OrderID);
}
Console.WriteLine("");
}

Create GroupBy Statements Dynamically

I am trying to dynamically re-structure some data to be shown in a treeview which will allows the user to select up to three of the following dimensions to group the data by:
Organisation
Company
Site
Division
Department
So for example, if the user were to select that they wanted to group by Company then Site then Division...the following code would perform the required groupings.
var entities = orgEntities
// Grouping Level 1
.GroupBy(o => new { o.CompanyID, o.CompanyName })
.Select(grp1 => new TreeViewItem
{
CompanyID = grp1.Key.CompanyID,
DisplayName = grp1.Key.CompanyName,
ItemTypeEnum = TreeViewItemType.Company,
SubItems = grp1
// Grouping Level 2
.GroupBy(o => new { o.SiteID, o.SiteName })
.Select(grp2 => new TreeViewItem
{
SiteID = grp2.Key.SiteID,
DisplayName = grp2.Key.SiteName,
ItemTypeEnum = TreeViewItemType.Site,
SubItems = grp2
// Grouping Level 3
.GroupBy(o => new { o.Division })
.Select(grp3 => new TreeViewItem
{
DisplayName = grp3.Key.Division,
ItemTypeEnum = TreeViewItemType.Division,
}).ToList()
}).ToList()
})
.ToList();
This would give a structre like this:
+ Company A
+ Site A
+ Division 1
+ Division 2
+ Site B
+ Division 1
+ Company B
+ Site C
+ Division 2
+ Company C
+ Site D
However, this only provides me with on of a large number of combinations.
How would I go about converting this into something that could create the equivalent expression dynamically based on the three dimensions that the user has chosen and so I don't have to create one of each of these expressions for each combination!!?
Thanks guys.
An intriguing problem. Choosing a single type for grouping keys and another type for results... makes it is very possible to get what you're asking for.
public struct EntityGroupKey
{
public int ID {get;set;}
public string Name {get;set;}
}
public class EntityGrouper
{
public Func<Entity, EntityGroupKey> KeySelector {get;set;}
public Func<EntityGroupKey, TreeViewItem> ResultSelector {get;set;}
public EntityGrouper NextGrouping {get;set;} //null indicates leaf level
public List<TreeViewItem> GetItems(IEnumerable<Entity> source)
{
var query =
from x in source
group x by KeySelector(x) into g
let subItems = NextGrouping == null ?
new List<TreeViewItem>() :
NextGrouping.GetItems(g)
select new { Item = ResultSelector(g.Key), SubItems = subItems };
List<TreeViewItem> result = new List<TreeViewItem>();
foreach(var queryResult in query)
{
// wire up the subitems
queryResult.Item.SubItems = queryResult.SubItems
result.Add(queryResult.Item);
}
return result;
}
}
Used in this way:
EntityGrouper companyGrouper = new EntityGrouper()
{
KeySelector = o => new EntityGroupKey() {ID = o.CompanyID, Name = o.CompanyName},
ResultSelector = key => new TreeViewItem
{
CompanyID = key.ID,
DisplayName = key.Name,
ItemTypeEnum = TreeViewItemType.Company
}
}
EntityGrouper divisionGrouper = new EntityGrouper()
{
KeySelector = o => new EntityGroupKey() {ID = 0, Name = o.Division},
ResultSelector = key => new TreeViewItem
{
DisplayName = key.Name,
ItemTypeEnum = TreeViewItemType.Division
}
}
companyGrouper.NextGrouping = divisionGrouper;
List<TreeViewItem> oneWay = companyGrouper.GetItems(source);
companyGrouper.NextGrouping = null;
divisionGrouper.NextGrouping = companyGrouper;
List<TreeViewItem> otherWay = divisionGrouper.GetItems(source);
Another option is to use DynamicLinq. If this is straight LINQ (not through some DB context such as LINQ2SQL), then this can be done by composing your grouping/selector strings:
var entities = orgEntities
.GroupBy("new(CompanyID, CompanyName)", "it", null) // DynamicLinq uses 'it' to reference the instance variable in lambdas.
.Select(grp1 => new TreeViewItem
{
...
.GroupBy("new(SiteID, o.SiteName)", "it", null)
// And so on...
You can probably abstract this into each of the criteria type. The only issue I see is the inner groupings might not be the easiest to compile together, but at least this can get you started in some direction. DynamicLinq allows you to build dynamic types, so it's certainly possible to abstract it even further. Ultimately, the biggest challenge is that based on what you're grouping by, the generated TreeViewItem contains different information. Good use case for dynamic LINQ, but the only problem I see is abstracting even further down the line (to the inner groupings).
Let us know what you come up with, definitely an interesting idea that I hadn't considered before.

Categories

Resources