Get recurring child result by depth level - c#

I have "googled" for many hour but still I am not able to find an answer. Basically, I have a table that looks like this:
Parent | ID
null | 1
1 | 2
2 | 3
3 | 4
3 | 5
3 | 6
4 | 7
null | 8
How can I use entity linq to filter based on Id and "depth level" (which is a simple count of elements including the Id element and n - 1 elements passed the Id element)?
For example when I pass Id 2 and depth level 2
Result will be
Parent | ID
2 | 3 //level 1
3 | 4 //level 2
3 | 5 //level 2
3 | 6 //level 2
If I pass Id 3 and depth level also 2
Result will be
Parent ID
3 | 4 //level 1
3 | 5 //level 1
3 | 6 //level 1
4 | 7 //level 2
Thanks for your help

I tired to simulate your scenario in a Console application. Following is one of the possible solution. Its very basic so you need to change and use it as you need. But it returns results as per logic you mention in your question.
class Program
{
static List<Data> data = new List<Data>();
static List<Data> result = new List<Data>();
static void Main(string[] args)
{
data.Add(new Data() { Parent = null, ID = 1 });
data.Add(new Data() { Parent = 1, ID = 2 });
data.Add(new Data() { Parent = 2, ID = 3 });
data.Add(new Data() { Parent = 3, ID = 4 });
data.Add(new Data() { Parent = 4, ID = 5 });
data.Add(new Data() { Parent = null, ID = 6 });
// Take() implementation is for Depth.
result = findChildren(3).Take(2).ToList();
Console.ReadLine();
}
static List<Data> findChildren(int Id)
{
return data.Where(x => x.Parent == Id ).Union(data.Where(x => x.Parent == Id).SelectMany(y => findChildren(y.ID))).ToList();
}
}
public class Data
{
public int? Parent { get; set; }
public int ID { get; set; }
}

var Id = 2;
var Level = 2;
var head = new Item { Id = Id };
var result = new List<Item>();
for(var i = 0; i < Level; i++)
{
head = context.Table.FirstOrDefault(x => x.ParetId == head.Id);
if(head != null)
result.Add(head);
else
break;
}

Related

Can I merge two lists using Linq?

I'm trying to merge two lists and I thought I had a solution but if there are two PackItems with the same length the results are not as expected.
Expectations/requirements.
Both lists contain the same total number of pieces for each length.
EDIT: Added code to clarify the input requirements.
The same length can be used in multiple PacksItems.
The same lengths can be produced out of multiple CoilNums.
The goal is to contain a list the contains a unique entry for each PackItem.ID/CoilNum.
Requirement for the output is that the total number of pieces for each length matched the input lists.
Here is the code I have so far.
public class PackItem
{
public int ID { get; set; }
public int Quantity { get; set; }
public string Length { get; set; }
}
public class ProductionInfo
{
public ProductionInfo AddID(PackItem item)
{
LineID = item.ID;
Quantity = Math.Min(Quantity, item.Quantity);
return this;
}
public int LineID { get; set; }
public string CoilNum { get; set; }
public int Quantity { get; set; }
public string Length { get; set; }
}
private void DoTest()
{
var packItems = new List<PackItem>()
{
new PackItem() {ID = 4, Quantity = 5, Length = "10"},
new PackItem() {ID = 5, Quantity = 2, Length = "4"},
new PackItem() {ID = 6, Quantity = 1, Length = "4"}
};
var productionInfoList = new List<ProductionInfo>()
{
new ProductionInfo() { CoilNum = "A", Quantity = 4, Length = "10"},
new ProductionInfo() { CoilNum = "B", Quantity = 1, Length = "10"},
new ProductionInfo() { CoilNum = "B", Quantity = 2, Length = "4"},
new ProductionInfo() { CoilNum = "A", Quantity = 1, Length = "4"},
};
//assert that both lists meet input requirements
var result1 = "";
var sum1 = packItems.GroupBy(i => i.Length);
foreach (var group in sum1) result1 += $"{group.Sum(i=>i.Quantity)} | {group.Key}\n";
var input2 = "";
var result2 = "";
var sum2 = productionInfoList.GroupBy(i => i.Length);
foreach (var group in sum2) result2 += $"{group.Sum(i => i.Quantity)} | {group.Key}\n";
Console.WriteLine("packItems: \nSum(Quantity) | Length");
Console.WriteLine(result1);
Console.WriteLine("productionInfoList: \nSum(Quantity) | Length");
Console.WriteLine(result2);
if (result1 == result2)
{
Console.WriteLine("Both Lists have the same quantity of each length");
}
else
{
Console.WriteLine("Error: Both Lists do not have the same quantity of each length");
return;
}
var merged = productionInfoList.SelectMany(x => packItems, (x, y) => new { x, y })
.Where(i => i.x.Length == i.y.Length)
.Select(i => i.x.AddID(i.y));
Console.WriteLine("ID | Coil | Qty | Length");
foreach (var item in merged)
{
Console.WriteLine($"{item.LineID} | {item.CoilNum} | {item.Quantity} | {item.Length}");
}
}
//expected output
ID | Coil | Qty | Length
4 | A | 4 | 10
4 | B | 1 | 10
5 | B | 2 | 4
6 | A | 1 | 4
//actual output
ID | Coil | Qty | Length
4 | A | 4 | 10
4 | B | 1 | 10
5 | B | 2 | 4
6 | B | 1 | 4
5 | A | 1 | 4
6 | A | 1 | 4
I'm stuck at this point and they only way I can think of is splitting each of these lists into individual items of one each, and then compiling a list by looping through them one by one.
Is there a way this can be done with Linq?
Here is a method that produces the correct output. Is there an easier way to do this? Can this be done with Linq only?
private void DoTest()
{
var packItems = new List<PackItem>()
{
new PackItem() {ID = 4, Quantity = 5, Length = "10"},
new PackItem() {ID = 5, Quantity = 2, Length = "4"},
new PackItem() {ID = 6, Quantity = 1, Length = "4"}
};
var productionInfoList = new List<ProductionInfo>()
{
new ProductionInfo() { CoilNum = "A", Quantity = 4, Length = "10"},
new ProductionInfo() { CoilNum = "B", Quantity = 1, Length = "10"},
new ProductionInfo() { CoilNum = "B", Quantity = 2, Length = "4"},
new ProductionInfo() { CoilNum = "A", Quantity = 1, Length = "4"},
};
//first create a list with one item for each pieces
var individualProduction = new List<ProductionInfo>();
foreach (var item in productionInfoList)
{
for (int i = 0; i < item.Quantity; i++)
{
individualProduction.Add(new ProductionInfo()
{
Quantity = 1,
Length = item.Length,
CoilNum = item.CoilNum
});
}
}
//next loop through and assign all the pack line ids
foreach (var item in individualProduction)
{
var packItem = packItems.FirstOrDefault(i => i.Quantity > 0 && i.Length == item.Length);
if (packItem != null)
{
packItem.Quantity -= 1;
item.LineID = packItem.ID;
}
else
{
item.Quantity = 0;
}
}
//now group them back into a merged list
var grouped = individualProduction.GroupBy(i => (i.CoilNum, i.LineID, i.Length));
//output the merged list
var merged1 = grouped.Select(g => new ProductionInfo()
{
LineID = g.Key.LineID,
CoilNum = g.Key.CoilNum,
Length = g.Key.Length,
Quantity = g.Count()
});
}
Quite unclear ...
This one is closed of the desired result but doesn't take into consideration any quantity so that the fist PackItem is always choosed. If decreasing the pItem.Quantity this would select the next available pItem.ID where Quantity > 0. But this will require more code :)
var results = productionInfoList.Select(pInfo =>
{
var pItem = packItems.First(z => z.Length == pInfo.Length);
return new { pItem.ID, pInfo.CoilNum, pInfo.Quantity, pInfo.Length };
}).ToList();
When you have a goal of : The goal is to contain a list the contains a unique entry for each PackItem.ID/CoilNum. your bottom answer is correct, since it has a unique id coilNum pair. What you are looking for is a different uniquenes.
var l = packItems.Join(productionInfoList, x => x.Length, y => y.Length, (x, y) => { y.AddID(x); return y; }).GroupBy(x => new { x.CoilNum, x.Length }).Select(x => x.First());
It is unclear on the exact rules of the case, but here I am using Length as a unique key to perform a join operation (Would recommend to have a different unique key for join operations).

Linq to group by two fields and average

I have the following C# models:
public class RawData
{
public int questionnaireId { get; set; }
public int coachNodeId { get; set; }
public int questionnaireNumber { get; set; }
public float score { get; set; }
}
public class AveragedData
{
public int coachNodeId { get; set; }
public int questionnaireNumber { get; set; }
public float averageScore { get; set; }
}
I have an API endpoint which is returning data from a database, mapped as List<RawData>. The values are like this:
questionnaireId | coachNodeId | questionnaireNumber | score
1 | 30 | 1 | 2
2 | 40 | 1 | 3
3 | 30 | 2 | 1
4 | 30 | 3 | 4
5 | 40 | 2 | 5
6 | 40 | 1 | 5
7 | 30 | 1 | 1
8 | 30 | 1 | 2
9 | 40 | 1 | 2
10 | 30 | 2 | 4
What I need to do now, in a LINQ query, is to average out the score values grouped by coachNodeId and questionnaireNumber and return a list of type AveragedData.
The values returned by averaging and grouping the example data above, should be:
coachNodeId | questionnaireNumber | averageScore
30 | 1 | 1.66666666 (calculated by: (2 + 1 + 2) / 3))
30 | 2 | 2.5 (calculated by: (1 + 4) / 2))
30 | 3 | 4 (calculated by: (4 / 1))
40 | 1 | 3.33333333 (calculated by: (3 + 5 + 2) / 3))
40 | 2 | 5 (calculated by: (5 / 1))
I'm not experienced with LINQ so am struggling to put together a query that groups by both coachNodeId and questionnaireNumber and averages the score, returning an object of type List<AveragedData>. Could anyone suggest how to accomplish this?
Many thanks.
assuming you have a List<RawData> called list, you are wanting:
var results = list.GroupBy(x => new
{
questionnaire = x.questionnaireId,
coach = x.coachNodeId
})
.Select(x => new AveragedData
{
coachNodeId = x.Key.coach,
questionnaireNumber = x.Key.questionnaire,
averageScore = x.Average(xx => xx.score)
})
.ToList();
Do the grouping, then use a Select to project the data to your type, using LINQ's Average as well.
Try following :
DataTable dt = new DataTable();
dt.Columns.Add("questionnaireId", typeof(int));
dt.Columns.Add("coachNodeId", typeof(int));
dt.Columns.Add("questionnaireNumber", typeof(int));
dt.Columns .Add("score", typeof(int));
dt.Rows.Add(new object[] {1,30, 1, 2});
dt.Rows.Add(new object[] {2,40, 1, 3});
dt.Rows.Add(new object[] {3,30, 2, 1});
dt.Rows.Add(new object[] {4,30, 3, 4});
dt.Rows.Add(new object[] {5,40, 2, 5});
dt.Rows.Add(new object[] {6,40, 1, 5});
dt.Rows.Add(new object[] {7,30, 1, 1});
dt.Rows.Add(new object[] {8,30, 1, 2});
dt.Rows.Add(new object[] {9,40, 1, 2});
dt.Rows.Add(new object[] {10,30, 2, 4});
var averages = dt.AsEnumerable()
.GroupBy(x => new { coachNodeId = x.Field<int>("coachNodeId"), questionnaireNumber = x.Field<int>("questionnaireNumber") })
.Select(x => new { coachNodeId = x.Key.coachNodeId, questionnaireNumber = x.Key.questionnaireNumber, average = x.Average(y => y.Field<int>("score")) })
.ToList();

LINQ select from join table where the same foreign key has records for two different IDs

Ok, so the title is a little bit confusing I guess. Basically I have those 3 tables:
Line
id | Name
---------
1 | "A-B"
2 | "A-D"
Stop
id | Name
---------
1 | A
2 | B
3 | C
4 | D
LineStop
Id | LineId | StopId | Order
----------------------------
1 | 1 | 1 | 0
2 | 1 | 2 | 1
3 | 2 | 1 | 0
4 | 2 | 2 | 1
5 | 2 | 3 | 3
4 | 2 | 4 | 4
So this is some sort of bus ticketing system which I work on of personal improvement.
As an input I get the departure StopId (Stop.Id) and the arrival StopId (Stop.Id). I want to select all lines that has those two stops in their routes (This would mean that in LineSop table for the same LineId I'll have records with both the departuring and arrival stops, ultimately I would also like to consider the Order column which tells in what order the bus is going through those Stops, because even if the line have the two stops I'm interested in, if they are in reversed order I'm still not interested.
I know that is highly desirable to show what I've done so far but I struggle with the where conditions which seems to be the key factor here. For some reason I decided to join Line with LineStop:
var lines = _context.Lines.Join(
_context.LineStop,
line => line.Id,
lineStop => lineStop.LineId,
(line, lineStop) => lineStop)
But then.. I need to check if for the same LineId I have records in LineStop table with the start and end StopId and ultimately when I found such records the the starting StopId Order i less than the end StopId Order.
I hope this can help you out:
First I get the trip from the traveler: "I want go from Stop: 2 to Stop:4".
Once I know the line that has both stops I build the stops and its order.
var lines = new List<Line>()
{
new Line() { Id = 1, Name = "A-B" },
new Line() { Id = 2, Name = "A-D" }
};
var stops = new List<Stop>() {
new Stop() { Id = 1, Name = "A" },
new Stop() { Id = 2, Name = "B" },
new Stop() { Id = 3, Name = "C" },
new Stop() { Id = 4, Name = "D" }
};
var lineStops = new List<LineStop>()
{
new LineStop() { Id = 1, LineId = 1, StopId = 1, Order = 0 },
new LineStop() { Id = 2, LineId = 1, StopId = 2, Order = 1 },
new LineStop() { Id = 3, LineId = 2, StopId = 1, Order = 0 },
new LineStop() { Id = 4, LineId = 2, StopId = 2, Order = 1 },
new LineStop() { Id = 5, LineId = 2, StopId = 3, Order = 3 },
new LineStop() { Id = 4, LineId = 2, StopId = 4, Order = 4 },
};
var result = (from trip in (from l in lines
join d in lineStops on l.Id equals d.LineId
join a in lineStops on l.Id equals a.LineId
where d.StopId == 2 && a.StopId == 4
select new { d.LineId })
join l in lines on trip.LineId equals l.Id
join ls in lineStops on l.Id equals ls.LineId
select new { l.Name, ls.StopId, ls.Order }).OrderBy(x => x.Order);
Expected result
Name StopId Order
A-D 1 0
A-D 2 1
A-D 3 3
A-D 4 4

Sort the two date columns individually and cumulative effect?

I have two date columns and two size columns (one size column related to one date column) like you can see in the Following table. Now I want the two arrays where in the first array it will be sort by Collected and show the cumulative effect of CollectedSize and same with Staged and StagedSize.
Required:
Collected array |
1/1/2016 | 1
11/1/2016 | 4
12/1/2016 | 6
30/1/2016 | 11
Staged array |
13/1/2016 | 3
14/1/2016 | 7
18/1/2016 | 13
16/1/2016 | 20
Table:
| Collected | CollectedSize | Staged | StagedSize |
| 11/1/2016 | 3 | 14/1/2016 | 4
| 12/1/2016 | 2 | 13/1/2016 | 3
| 30/1/2016 | 5 | 18/1/2016 | 7
| 01/1/2016 | 1 | 16/1/2016 | 6
Currently using the following code:
public class ProductionDataOverTimeVM
{
public ProductionDataOverTimeVM()
{
Collected = new List<TimeChartXAxis>();
Staged = new List<TimeChartXAxis>();
}
public List<TimeChartXAxis> Collected { get; set; }
public List<TimeChartXAxis> Staged { get; set; }
}
public class TimeChartXAxis
{
public string x { get; set; }
public string y { get; set; }
}
var queryResults = context.Datasets.ToList();
ProductionDataOverTimeVM obj = new ProductionDataOverTimeVM();
long? collectedBytes = 0;
long? Staged = 0;
foreach (var dataset in queryResults.OrderBy(d => d.Collected))
{
if (dataset.Collected != null)
{
collectedBytes = collectedBytes + dataset.CollectedSize;
obj.Collected.Add(new TimeChartXAxis
{
x = dataset.Collected != null ? BasicHelpers.FromUTCDate(dataset.Collected, parms.Offset).Value.ToString("dd/M/yyyy") : null,
y = BasicHelpers.FormatBytesToSpecificFormat(collectedBytes, format, false)
});
}
}
foreach (var dataset in queryResults.OrderBy(d => d.Staged))
{
if (dataset.Staged != null)
{
Staged = Staged + dataset.StagedSize;
obj.Staged.Add(new TimeChartXAxis
{
x = dataset.Staged != null ? BasicHelpers.FromUTCDate(dataset.Staged, parms.Offset).Value.ToString("dd/M/yyyy") : null,
y = BasicHelpers.FormatBytesToSpecificFormat(Staged, format, false)
});
}
}
What will be the best approach to do that?
What about
var arrayofOrderByString = new []{"Collected","Staged"}
foreach(var key in arrayofOrderByString){
var y=0;
SortList<Datasets>(queryResults, key, SortDirection.Descending);
queryResults.foreach(s =>{
y=s.GetType().GetProperty(key).GetValue(s, null);
obj.Collected.Add(new ProductionDataOverTimeVM{
x =BasicHelpers.FromUTCDate(s.GetType().GetProperty(key).GetValue(s, null), parms.Offset).Value.ToString("dd/M/yyyy"),
y=collectedBytes
})
})
}
public void SortList<T>(List<T> list, string columnName, SortDirection direction)
{
var property = typeof(T).GetProperty(columnName);
var multiplier = direction == SortDirection.Descending ? -1 : 1;
list.Sort((t1, t2) => {
var col1 = property.GetValue(t1);
var col2 = property.GetValue(t2);
return multiplier * Comparer<object>.Default.Compare(col1, col2);
});
}

Populate XtraTreeList with dynamic nodes

I have table with two columns:
+-------------+------------+
| Level | Desc |
+-------------+------------+
| 1 | a |
+-------------+------------+
| 2 | b |
+-------------+------------+
| 2 | c |
+-------------+------------+
| 1 | d |
+-------------+------------+
| 2 | e |
+-------------+------------+
| 2 | f |
+-------------+------------+
| 3 | g |
+-------------+------------+
| 1 | h |
+-------------+------------+
| 1 | i |
+-------------+------------+
| 2 | j |
+-------------+------------+
| 2 | k |
+-------------+------------+
And I need to create display of this data in XtraTreeview with two columns according to Level column and it should be like:
- 1 a
-- 2 b
-- 2 c
-1 d
-- 2 e
-- 2 f
-- 3 g
-1 h
-1 i
-- 2 j
-- 2 k
So, level columns represents the node. Level 1 is the main node, level 2 is subnode of level 1, level 3 is subnode of level 2, level 4 is subnode of 3...
I know how to populate Xtratreeview when there is fixed numbers of nodes and subnodes but in this case don't have idea how to populate where 1 node consist 3, 4 or more subnodes.
I've done this so far:
Populate TreeView:
DataTable table = new DataTable();
table.Columns.Add("Level");
table.Columns.Add("Data");
table.Rows.Add(1, "a");
table.Rows.Add(2, "b");
table.Rows.Add(2, "c");
table.Rows.Add(1, "d");
table.Rows.Add(2, "e");
table.Rows.Add(2, "f");
table.Rows.Add(3, "g");
table.Rows.Add(4, "z");
table.Rows.Add(5, "x");
table.Rows.Add(2, "h");
table.Rows.Add(3, "i");
table.Rows.Add(1, "j");
table.Rows.Add(2, "k");
TreeListNode rootNode = null;
for (int i = 0; i < table.Rows.Count; i++)
{
tl.BeginUnboundLoad();
TreeListNode parentForRootNodes = null;
if (table.Rows[i][0].ToString().Equals("1"))
{
rootNode = tl.AppendNode(new object[] { (string)table.Rows[i][1] }, parentForRootNodes);
}
if (table.Rows[i][0].ToString().Equals("2"))
{
tl.AppendNode(new object[] { (string)table.Rows[i][1] }, rootNode);
}
tl.EndUnboundLoad();
}
Create columns:
private void CreateColumns2(TreeList tl)
{
tl.BeginUpdate();
tl.Columns.Add();
tl.Columns[0].Caption = "Level";
tl.Columns[0].VisibleIndex = 0;
tl.Columns.Add();
tl.Columns[1].Caption = "Desc";
tl.Columns[1].VisibleIndex = 1;
tl.Columns.Add();
tl.EndUpdate();
}
Documentation you might like to read is here: https://documentation.devexpress.com/#windowsforms/CustomDocument198
You need at minimum three things for a tree:
Id
ParentId
Text
So the structure you've described needs to change to permit finding the parent for an item.
Once you have that, the concept goes like this:
Create an item for each node you want in the tree, I created my own class for this with the properties I wanted (Id, ParentId, Text...)
Then set the datasource of the tree control
Example:
var data = new List<TreeItem>
{
new TreeItem { Id = "L1_1", ParentId = "", Text = "ONE" },
new TreeItem { Id = "L1_2", ParentId = "", Text = "TWO" },
new TreeItem { Id = "L1_3", ParentId = "", Text = "THREE" },
new TreeItem { Id = "L2_1", ParentId = "L1_1", Text = "A" },
new TreeItem { Id = "L2_2", ParentId = "L1_1", Text = "B" },
new TreeItem { Id = "L2_3", ParentId = "L1_2", Text = "C" },
new TreeItem { Id = "L2_4", ParentId = "L1_2", Text = "D" },
new TreeItem { Id = "L2_5", ParentId = "L1_2", Text = "E" }
};
tree.Properties.DataSource = data;
}
class TreeItem
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Text { get; set; }
}
The order of the items in the data source is irrelevant, what is important is the uniqueness of each id.
The above example produces a tree like this:
- ONE
-- A
-- B
- TWO
- THREE
-- C
-- D
-- E
I am doing this without my DevExpress installation and without a compiler, so please excuse any errors. however the concept remains the same.

Categories

Resources