I have following data Structure and i want to put covert it in a Hierarchy based on RelationId. This is sorted on RelationId.
Id = 2 has relationId =2 and following two rows has realtionId =0 . That represent the Id=3 and Id=4 are child of Id = 2
Id Name RelationId SortOrder
1 A 1 1
2 B 2 2
3 C 0 3
4 D 0 4
5 E 3 5
6 F 0 6
7 G 0 7
8 H 4 8
End Result would be like following
Id = 1
|
Id = 2
|___ Id = 3 , Id = 4
Id = 5
|___ Id= 6 , Id=7
Id = 8
The desired result is as following (for simplicity representing it as List). This would be a List<Something> in C#
Result =
[
{ Id = 1, Name = A, Children = Null },
{ Id = 2, Name = B, Children = [{ Id = 3, Name = C }, {Id = 4, Name = D }] },
{ Id = 5, Name = E, Children = [{ Id = 6, Name = F }, {Id = 7, Name = G }] },
{ Id = 8, Name = H}
]
My unsuccessful attempt is as following
var finalResult = new List<sampleDataClass>();
var sampleData = GetMeSampleData();
var count = sampleData.Count();
foreach (var item in sampleData)
{
var alreadyExist = finalResult.Any(x => x.Id == item.Id);
var newObject = new sampleDataClass();
if (!alreadyExist && item.RelationId!= 0)
{
newObject = item;
}
for (int i = item.SortOrder; i < count; i++)
{
if (sampleData[i].RelationId== 0)
{
newObject.Children.Add(sampleData[i]);
}
}
finalResult.Add(newObject );
}
Since your RelationId decides whether it is a root or nested element, you could form a group based on these relation and do this. I would suggest using Linq
List<SomeData> somedata = ... // your data.
int index=0;
var results = somedata
.Select(x=> new {gid = x.RelationId ==0? index: ++index, item=x})
.GroupBy(x=> x.gid)
.Select(x=> {
var first = x.FirstOrDefault();
return new
{
Id = first.item.Id,
Name = first.item.Name,
Children = x.Skip(1).Select(s=> new {
Id = s.item.Id,
Name = s.item.Name,
})
};
})
.ToList();
Output :
Id=1, Name=A
Id=2, Name=B
Id=3, Name=C
Id=4, Name=D
Id=5, Name=E
Id=6, Name=F
Id=7, Name=G
Id=8, Name=H
Check this Working Code
I have done it like this. Don't know if there can be some more elegant solution
var data = new List<MyDataObject>();
var SampleData = GetMeSampleData;
var count = SampleData.Count();
for (int i=0;i<count;i++)
{
var rootAdded = false;
var relationId = SampleData[i].relationId;
var alreadyExist = data.Any(x => x.Id == SampleData[i].Id);
var mydataObject = new MyDataObject();
if (!alreadyExist && SampleData[i].RelationId != 0)
{
mydataObject = SampleData[i];
rootAdded = true;
}
for(int j=i+1;j<count;j++)
{
if ((SampleData[j].RelationId == 0 && rootAdded))
{
mydataObject.Children.Add(SampleData[j]);
}
if (SampleData[j].SubjectId != 0)
break;
}
if (rootAdded)
{
data.Add(mydataObject);
}
Related
I have a list like the one below. I want to sort this list according to the rule I specified. If there is an element to which the list element is attached, it must be in the front row.
For example, since the RequiredCourse field of the record with Id 2 is 3, the element with Id 3 must come before the element with Id 2. To do this, I created a swap method as follows. But the record with Id 3 depends on 4 and I couldn't create the sorting properly. Additionally, it depends on 10 with Id 8 and below. But 9 is not connected to anything. Since I changed the place of the records with id 8 and 10, 8 goes after 9.
The sample output should be as follows:
var list = new List<Course>();
list.Add(new Course { Id = 1 });
list.Add(new Course { Id = 2, RequiredCourse = "3" });
list.Add(new Course { Id = 3, RequiredCourse = "4" });
list.Add(new Course { Id = 4 });
list.Add(new Course { Id = 5 });
list.Add(new Course { Id = 6 });
list.Add(new Course { Id = 7 });
list.Add(new Course { Id = 8, RequiredCourse = "10" });
list.Add(new Course { Id = 9 });
list.Add(new Course { Id = 10 });
list = list.OrderBy(i => i.Id).ThenBy(i => i.RequiredCourse).ToList();
var copy = new List<Course>(list);
for (int i = 0; i < copy.Count; i++)
{
if (!string.IsNullOrEmpty(copy[i].RequiredCourse) && Int32.Parse(copy[i].RequiredCourse) > copy[i].Id)
{
var index = list.FindIndex(k => k.Id == Int32.Parse(copy[i].RequiredCourse));
if (index > -1)
{
var temp = list[i];
list[i] = list[index];
list[index] = temp;
}
}
}
Defective Output using code above
1
3 4
4
2 3
5
6
7
10
9
8 10
Expected Output
1
4
3 4
2 3
5
6
7
10
8 10
9
I think this is easy enough to understand.
var list = new List<Course>();
list.Add(new Course { Id = 1 });
list.Add(new Course { Id = 2, RequiredCourse = "3" });
list.Add(new Course { Id = 3, RequiredCourse = "4" });
list.Add(new Course { Id = 4 });
list.Add(new Course { Id = 5 });
list.Add(new Course { Id = 6 });
list.Add(new Course { Id = 7 });
list.Add(new Course { Id = 8, RequiredCourse = "10" });
list.Add(new Course { Id = 9 });
list.Add(new Course { Id = 10 });
var output = new List<Course>();
var toProcess = new SortedDictionary<int, Course>(list.ToDictionary(c => c.Id));
var pendingAdd = new Stack<Course>();
while (toProcess.Count > 0)
{
Course currentCourse = toProcess.First().Value;
if (string.IsNullOrEmpty(currentCourse.RequiredCourse))
{
// Course has no dependency, process it.
output.Add(currentCourse);
toProcess.Remove(currentCourse.Id);
}
else
{
int courseId = currentCourse.Id;
// Course has dependency. Trace down linked courses.
while (toProcess.ContainsKey(courseId) && !string.IsNullOrEmpty(toProcess[courseId].RequiredCourse))
{
pendingAdd.Push(toProcess[courseId]);
courseId = int.Parse(toProcess[courseId].RequiredCourse);
}
// dont forget to add the "top-level" course for the dependency chain
pendingAdd.Push(toProcess[courseId]);
// Add in reverse depdency order using Stack
while (pendingAdd.Count > 0)
{
var course = pendingAdd.Pop();
output.Add(course);
toProcess.Remove(course.Id);
}
}
}
Your wanted list is in output.
The problem is that you want the item "2 - 3" below the "4 - ''". I'm sure there's a way to improve what I've done, but here's version 1.0:
var list = new List<Course>();
list.Add(new Course { Id = 1 });
list.Add(new Course { Id = 2, RequiredCourse = "3" });
list.Add(new Course { Id = 3, RequiredCourse = "4" });
list.Add(new Course { Id = 4 });
list.Add(new Course { Id = 5 });
list.Add(new Course { Id = 6 });
list.Add(new Course { Id = 7 });
list.Add(new Course { Id = 8, RequiredCourse = "10" });
list.Add(new Course { Id = 9 });
list.Add(new Course { Id = 10 });
// isolating items with RequiredCourse
var withRequired = (
from o in list
where !string.IsNullOrWhiteSpace(o.RequiredCourse)
select o
).ToList();
list = list.Except(withRequired).ToList();
// First I arrange the items without "RequiredCourse" with their respective ones.
for (var i = 0; i < list.Count; i++)
{
var primary = list[i];
var parents =
withRequired
.Where(o => o.RequiredCourse == primary.Id.ToString())
.OrderBy(o => o.Id)
.ToList();
var children = (
from o in withRequired.Except(parents)
join o2 in parents
on o.Id.ToString() equals o2.RequiredCourse
into grp
from o3 in grp
orderby o3.Id descending
select o3
).ToList();
if (!parents.Any())
{
continue;
}
i++;
foreach (var p in parents)
{
list.Insert(i++, p);
foreach (var c in children.Where(o => p.Id.ToString() == o.RequiredCourse))
{
list.Insert(i++, c);
}
}
}
// retrieving items that only link to items that have RequiredCourse
var childOfChildren = withRequired.Except(list).ToList();
// Then I arrange items with RequiredCourses that are linked to other items with RequiredCourses.
for (var i = 0; i < list.Count; i++)
{
var primary = list[i];
var children = (
from o in childOfChildren
where primary.Id.ToString() == o.RequiredCourse
orderby o.Id descending
select o
).ToList();
if (!children.Any())
{
continue;
}
i++;
list.InsertRange(i, children);
}
// CONSOLE APPLICATION PART
foreach (var o in list)
{
if (string.IsNullOrEmpty(o.RequiredCourse))
{
o.RequiredCourse = "-";
}
Console.WriteLine($"Course: {o.Id} - {o.RequiredCourse}");
}
Can be done with regression , sample code not tested for other case . only tested for given data .
public static void Main()
{
var list = new List<Course>();
list.Add(new Course { Id = 1 });
list.Add(new Course { Id = 2, RequiredCourse = "3" });
list.Add(new Course { Id = 3, RequiredCourse = "4" });
list.Add(new Course { Id = 4 });
list.Add(new Course { Id = 5 });
list.Add(new Course { Id = 6 });
list.Add(new Course { Id = 7 });
list.Add(new Course { Id = 8, RequiredCourse = "10" });
list.Add(new Course { Id = 9 });
list.Add(new Course { Id = 10 });
list = list.OrderBy(i => i.Id).ThenBy(i => i.RequiredCourse).ToList();
var copy = new List<Course>();
foreach(var tt in list)
{
//Console.WriteLine(tt.Id + " " + tt.RequiredCourse);
populateParent(list,ref copy, tt);
if(!copy.Any(p => p.Id == tt.Id))
copy.Add(tt);
}
foreach(var tt in copy)
{
Console.WriteLine(tt.Id + " " + tt.RequiredCourse);
}
}
public static Course populateParent(List<Course> inputList,ref List<Course> outputList, Course current)
{
if(string.IsNullOrWhiteSpace(current.RequiredCourse))
{
if(!outputList.Any(p => p.Id == current.Id))
outputList.Add(current);
return current;
}
else
{
var tt = inputList.First(p => current.RequiredCourse != null && p.Id == int.Parse(current.RequiredCourse) );
var parent = populateParent(inputList,ref outputList,tt);
if(!outputList.Any(p => p.Id == parent.Id))
outputList.Add(parent);
if(!outputList.Any(p => p.Id == tt.Id))
outputList.Add(tt);
return tt;
}
}
code in dotnetfiddle
I have problem with Linq, the code used to be processed in sql via stored procedure, but right now it supposed to be on code via linq, here's my database schema
SQL Fiddle
what I want is , from this data
orderNo
Type
serial
1
BN
BN 1
1
BE
BE 1
2
BN
BN 2
2
BE
BE 2
3
BN
BN 3
3
BE
BE 3
to be like this :
orderNo
be
bn
1
BE 1
BN 1
2
BE 2
BN 3
3
BE 2
BN 3
found one question and solution Source 1 - Stackoverflow , when I tried to my code, I got an issue with SelectMany
here's what I've tried
var results = data_tech.GroupBy(l => l.serial).SelectMany( g =>
new
{
Metadata = g.Key,
data = g
});
var pivoted = new List<PivotedEntity>();
foreach(var item in results)
{
pivoted.Add(
new PivotedEntity
{
Order= item.orderNo,
BE= item.data.Where(x => x.Name == "BE")
.FirstOrDefault().value,
BN= item.data.Where(x => x.Name == "BN")
.FirstOrDefault().value,
});
}
You can simply achieve this by changing the group by element serial to OrderNo. Let me give you an example,
var list = new List<Order>() {
new Order { orderNo = 1, Type = "BN", Serial = "BN 1" },
new Order { orderNo = 1, Type = "BE", Serial = "BE 1" },
new Order { orderNo = 2, Type = "BN", Serial = "BN 2" },
new Order { orderNo = 2, Type = "BE", Serial = "BE 2" },
new Order { orderNo = 3, Type = "BN", Serial = "BE 3" } ,
new Order { orderNo = 3, Type = "BE", Serial = "BN 3" } };
var results = list.GroupBy(l => l.orderNo).Select(g =>
new
{
Metadata = g.Key,
data = g
});
var pivoted = new List<PivotedEntity>();
foreach (var item in results)
{
pivoted.Add(
new PivotedEntity
{
Order = item.Metadata,
BE = item.data.Where(x => x.Type == "BE")
.FirstOrDefault().Serial,
BN = item.data.Where(x => x.Type == "BN")
.FirstOrDefault().Serial,
});
}
This will give you some output like this image.
Edit: Output PivotedEntity class =>
internal class PivotedEntity
{
public int Order { get; set; }
public string BE { get; set; }
public string BN { get; set; }
}
This question already has answers here:
Group by in LINQ
(11 answers)
Closed 5 years ago.
I'm trying to select a list that contains Fund.Name and List<Investment>.
var funds = new List<Fund>
{
new Fund { Id = 1 , Name = "good" },
new Fund { Id = 2, Name = "bad" }
};
var investments = new List<Investment>
{
new Investment { Fund = funds[0], Value = 100 },
new Investment { Fund = funds[0], Value = 200 },
new Investment { Fund = funds[1], Value = 300 }
};
Then I'm trying to create the query with this:
var query = from f in funds
join i in investments
on f.Id equals i.Fund.Id
select new { f.Name, i };
I wanted something like this:
{ Name = good, {{ Id = 1, Value = 100 }, { Id = 1, Value = 200 }}},
{ Name = bad, { Id = 2, Value = 300 }}
But I'm getting something like this:
{ Name = good, { Id = 1, Value = 100 }},
{ Name = good, { Id = 1, Value = 200 }},
{ Name = bad, { Id = 2, Value = 300 }}
Try using GroupJoin.
var query = funds.GroupJoin(investments, f => f.Id, i => i.Fund.Id, (f, result) => new { f.Name, result });
I have 2 lists of the same type.
List 1:
ID
Name
Value
1,"Prod1", 0
2,"Prod2", 50
3,"Prod3", 0
List 2:
ID
Name
Value
1,"Prod1", 25
2,"Prod2", 100
3,"Prod3", 75
I need to combine these 2 lists into 1, but I only want the values from list2 if the corresponding value from list1 == 0
So my new list should look like this:
1,"Prod1", 25
2,"Prod2", 50
3,"Prod3", 75
I've tried many variations of something like this:
var joined = from l1 in List1.Where(x=>x.Value == "0")
join l2 in List2 on l1.ID equals l2.ID into gj
select new { gj };
I've also tried a variation of the concat
What is the best way of doing this?
You just need to select the individual properties and conditionally select either the Value from the first or second list item.
var List1 = new[]
{
new { Name = "Prod1", Id = 1, Value = 0 },
new { Name = "Prod2", Id = 2, Value = 50 },
new { Name = "Prod3", Id = 3, Value = 0 },
new { Name = "NotInList2", Id = 4, Value = 0}
};
var List2 = new[]
{
new { Name = "Prod1", Id = 1, Value = 25 },
new { Name = "Prod2", Id = 2, Value = 100 },
new { Name = "Prod3", Id = 3, Value = 75 }
};
var results = from l1 in List1
join l2temp in List2 on l1.Id equals l2temp.Id into grpj
from l2 in grpj.DefaultIfEmpty()
select new
{
l1.Id,
l1.Name,
Value = l1.Value == 0 && l2 != null ? l2.Value : l1.Value
};
foreach(var item in results)
Console.WriteLine(item);
Will output
{ Id = 1, Name = Prod1, Value = 25 }
{ Id = 2, Name = Prod2, Value = 50 }
{ Id = 3, Name = Prod3, Value = 75 }
{ Id = 4, Name = NotInList2, Value = 0 }
NOTE: This assumes that you only want all the ids that are in List1 (not any that are only in List2) and that the ids are unique and that the Name from List1 is what you want even if it is different in List2.
clone l1 and
foreach (var item in l1Clone)
if (item.value == 0)
item.value == l2.FirstOrDefault(l2item => l2item.ID == item.ID)
Refer to the code below:
IEnumerable<item> join_lists(IEnumerable<item> list1, IEnumerable<item> list2)
{
var map = list2.ToDictionary(i => i.id);
return list1.Select(i => new item()
{
id = i.id,
name = i.name,
value = i.value == 0 ? map[i.id].value : i.value
});
}
You could use Zip:
var combined = list1
.Zip(list2, (product1, product2) => product1.Value == 0 ? product2 : product1);
I have a table
ID|VALUE
VALUE is an integer field with possible values between 0 and 4. How can I query the count of each value?
Ideally the result should be an array with 6 elements, one for the count of each value and the last one is the total number of rows.
This simple program does just that:
class Record
{
public int Id { get; set; }
public int Value { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Record> records = new List<Record>()
{
new Record() { Id = 1, Value = 0},
new Record() { Id = 2, Value = 1 },
new Record() { Id = 3, Value = 2 },
new Record() { Id = 4, Value = 3 },
new Record() { Id = 5, Value = 4 },
new Record() { Id = 6, Value = 2 },
new Record() { Id = 7, Value = 3 },
new Record() { Id = 8, Value = 1 },
new Record() { Id = 9, Value = 0 },
new Record() { Id = 10, Value = 4 }
};
var query = from r in records
group r by r.Value into g
select new {Count = g.Count(), Value = g.Key};
foreach (var v in query)
{
Console.WriteLine("Value = {0}, Count = {1}", v.Value, v.Count);
}
}
}
Output:
Value = 0, Count = 2
Value = 1, Count = 2
Value = 2, Count = 2
Value = 3, Count = 2
Value = 4, Count = 2
Slightly modified version to return an Array with only the count of values:
int[] valuesCounted = (from r in records
group r by r.Value
into g
select g.Count()).ToArray();
Adding the rows count in the end:
valuesCounted = valuesCounted.Concat(new[] { records.Count()}).ToArray();
Here is how you would get the number of rows for each value of VALUE, in order:
var counts =
from row in db.Table
group row by row.VALUE into rowsByValue
orderby rowsByValue.Key
select rowsByValue.Count();
To get the total number of rows in the table, you can add all of the counts together. You don't want the original sequence to be iterated twice, though; that would cause the query to be executed twice. Instead, you should make an intermediate list first:
var countsList = counts.ToList();
var countsWithTotal = countsList.Concat(new[] { countsList.Sum() });