I used LINQ to solve the following problem: find all titles of objects at hierarchy level X when you know object ID in hierarchy level Y. I cannot imagine if there is some other solution that is same short and clear as one that LINQ provides. Take a look and decide by yourself.
Here is simple diagram with my entities. Here are my simple rules. Level1 has no parent level and my contain one ore more Level2 entities. Level2 entities have one Level1 parent and one or more Level3 enitities. Level3 entities have one Level2 parent entity and collection of one or more Items. So there is many-to-many relationship between Level3 and Items.
We cannot use composite pattern here because these classes will be very different and there will be no point where we need one common interface for them. That’s why we have one class per level. Also the number of levels is fixed and there is no plan to expand this hierarchy.
By the way, you can model arbitrary class hierarchies on this model and still use this example (as long as it doesn’t hurt performance and you are really sure what you are doing).
Excercise: having Level1 items collection and knowing Level3 item ID find all Items for specified Level3 item and return string of their titles separated by comma. As you don’t have access to source code of data source you must use IList<Level1> and LINQ.
We will use simple class structure given below and we expect that we already got list of Level1 items from some repository or data context.
Now it’s time to write some hardcore loops and create a cool code-hell… or maybe it’s time to be elegant and use LINQ as stated before. Using LINQ we can provide the following solution:
BEGIN TRY BEGIN TRAN --put queries here COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT>0 BEGIN SELECT @@ERROR,ERRO...
Here are some of the string formats that you can try out: Specifier type output (June 8, 1970 12:30:59) dd Day 08 ddd ...
One of my friends contacted me recently and said they needed some help creating a stored procedure that imported data from a text file. They...
How to find the all child of particular parent using the Recursive Query --here we are creating the table variable so that we can insert so...