I am loading all first level/root units into my Treeview.
Whether a unit has children or not determines whether the treeview shows "+".
In order to make this symbol available on the left hand side of the unit name I need to check on my SQL server whether each unit has children or not. I do not need the count, just whether children exist is enough.
I even know there was once a very good rated SO solution about the same topic, but I could not find this solution with the search.
I would appreciate the solution or any helpful links :)
Code follows:
public IEnumerable<Unit> GetRootUnits(int templateId)
{
List<Unit> units = new List<Unit>();
using (var con = new SqlConnection(_connectionString))
using (var cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT UnitId, ParentId, Name, TemplateId, HasChildren FROM Unit WHERE ParentId Is NULL AND TemplateId = @TemplateId";
con.Open();
var p1 = new SqlParameter("@TemplateId", templateId);
cmd.Parameters.Add(p1);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Unit unit = new Unit();
unit.Id = Convert.ToInt32(reader["UnitId"]);
unit.Name = reader["Name"].ToString();
unit.TemplateId = Convert.ToInt32(reader["TemplateId"]);
unit.ParentId = null;
unit.IsLazy = Convert.ToBoolean(reader["HasChildren"]);
units.Add(unit);
}
}
}
return units;
}
Update:
I tried this:
SELECT Unit.UnitId, Unit.ParentId, Unit.Name, Unit.TemplateId,
case when Exists(select null from Unit u2 where u2.ParentID = unit.UnitID)
then 1
else 0
end as HasChildren
FROM [ITMS].[dbo].[Unit]
My test data:
1 NULL unitroot1 1
2 NULL unitroot2 1
3 NULL unitroot3 1
4 NULL unitroot4 1
5 NULL unitroot5 1
6 NULL unitroot6 1
7 NULL unitroot7ffds fasddfasfds fsdadsffdsa fasdadsf 1
8 NULL flfsaklfakl 1
9 NULL flk43053094 1
10 NULL sdaklr0340 1
11 NULL 3405303 1
12 NULL 543ß5ß343 1
13 NULL 54ß53ß534 1
14 NULL 45o345jo435jo 1
15 NULL as435l5l54lk5 1
16 NULL 543095454j34 1
17 NULL 45354 1
18 NULL kl 1
19 NULL 43534 1
20 NULL 5435 1
21 NULL 1 1
22 NULL 12 1
23 NULL bla 1
24 7 childrenOfUnitRoot7 this is a scrolling test 1
25 24 blubb 1