Loading a hierarchy with one hit to a database using Linq - c#

We have a table structure like an order table with an order lines table and we define a hierarchy of items in the table according to the user requirements by storing a string made up of a comma separated list of record id’s at each level. This allows the user to “re-order” their lines as needed.
e.g.
A Hierarchy string on the order could be something like “3,2,4” meaning that its top level children are lines 3,2,4 (in that order).
The first order line might have its own hierarchy string such as “5,6” indicating that it has nested children that should appear below it.
We want to return this data structure as a flat list of objects in order with a marker of how “deep” it is in the hierarchy. For example if the data looks like this:
Header:
Lines:
We want a flat list like this (in this order):
{Line1Object}, 1
{Line3Object}, 2
{Line2Object}, 2
{Line4Object}, 2
{Line5Object}, 3
{Line6Object}, 3
Can this be done in one hit with some kind of joining process in Entity Framework instead of looping through our records and building this up as we go or are we being too ambitious?

Related

How to combine 2 models into 1 for ObjectListView?

I have the following model, which is central model and used in ObjectListView
Cells(List)
Number (same as Cell Number)
Status
Lots of properties
It looks like this:
I also have the following model, which is used in other ObjectListView.
Tests (List)
Cell Number
Enabled
Warning
Passes
Name
It looks like this:
What I want is to modify first model model to feed both ObjectListViews in order to avoid redundancy:
Ideally the new model would look like this:
Cells (List)
Number (same as Cell Number)
Status
Lots of properties
Tests (List)
But then it's not clear how to feed it to second ObjectListView.
Question - how to combine these 2 models in order to be able feed OLV to have the result like on screenshots.
Correct me if I'm wrong, but there is no way for the ObjectListView to generate multiple row from a single model object.
You'll need to generate the IENUMERABLE of Tests at one point.
You can let the OLV do this by iterating the list of Cells and adding (olv.addObjects) each ienumerable of tests to the secondary ObjectListView.

How can I simply store a number of distribution list criteria/logic in database?

I need to select a distribution list from a list stored in the database based upon two inputs which are the following
A single customer type (Business or Residential, one or the other, not both, null is not a valid input)
A single service area (SA1,SA2,SA3,SA4, one and only one)
The logic for determining which list to select is simple and can be determined within a stored procedure.
However the problem comes when attempting to store the determining criteria for each distribution list. The criteria for a distribution list can be any combination of customer types (residential only, business only, residential and business) and service area (for example Residential SA1 is different from Business SA1, is different from Business and Residential SA2). My initial solution was to use two columns customerType and ServiceArea and have comma separate lists that contained the customer types and service area for each distribution list. I would like to avoid that so that my database can maintain some semblance of 3NF.I would also like to avoid have a column for each customer type and each service area.
Is there a way to store this information without comma separated lists and without a column for each value, so that a stored procedure given a single customer type and a single service area select the distribution list that is tied to that combination of customer type and service area?
There is a good reason not to stuff the service areas in a comma delimited list in most situations, however, you are storing a user preference setting so the Normal Forms can be relaxed a bit if the service area codes are never removed or renamed and only added to. I look at this like storing a user's color preference if it does not cause the system to crash then do what is easiest.
Having said that, if SA1,SA2,SA3... are subject to removal and/or renaming then I would not use a list for this. Some relationship similar to below would work if you are required to use 3NF.
CustomerServiceAreaFilter
CustomerServiceAreaFilterID(PK)
UserID(FK)
CustomerType(FK)
**Unique Index On UserID and CustomerType
CustomerServiceAreaFilterServiceArea
CustomerServiceAreaFilterServiceAreaID(PK)
CustomerServiceAreaFilterID(FK)
ServiceAreaID(FK)
**Uniqe Index on CustomerServiceAreaFilterID ServiceArea
Assuming your customer type and service areas have unique numbers associated with them, and that you know the upper bound of each, you could say that each customer's properties can be turned into a number by doing:
2 ^ customer type + (2 ^ service area) * 2 ^ (maximum customer type value)
In practice, for your customers this means each customer will have something like the following:
100010
100001
010010
010001
001010
001001
left 4 digits are binary of service area, right 2 digits are customer type
Your criteria table could be one with three columns:
| distribution_list_id | criteria_id | group |
When determining the members of the distribution, query your criteria table for all rows matching a distribution list's id, grouping by the group column and selecting sum of criteria_id. Select all rows from customers join to this criteria table on their calculated bit flags being equal to the customer's type. The purpose of the group column is to allow you to and things together, whereas separate rows with different group values in the criteria table indicate or.
Assuming you write well-documented stored procedures to insert into these tables, the whole "changing-into-bit-logic" part shouldn't get too hairy, and the purpose is to allow you to easily do and of arbitrary numbers of criteria based on group (sum is much easier than string concatenation in a group by). This way, when inevitably customers get more properties like they always do, you can just add more bits to the end for another set of properties without changing your schema.
Full disclosure, I'm also not very good at databases but I think this satisfies normal form? If not please let me know why in the comments, also trying to learn.
-Edited for typos

Storing precedence of operations in database? Patterns?

I have a need to store a list of operations and their precedence or order in the database.
I am unsure whether to store operations with the highest precedence as ZERO or otherwise.
In other words,
should the order of operations in precedence be stored in the database as :
'0 1 2 3 4 5 6'
OR
'6 5 4 3 2 1 0'
TABLE DATA STRUCTURE:
[operationName, precedence]
Also, a user can re-order them etc from the UI(a WPF app) in a list and hit "Save", in which case I will need to update them. He can also create a new operation and insert it into the list in the UI and hit SAVE ... which should also store the operations in the database according to the order in the UI.
EDIT:
Other things are they can even remove or delete them from the UI which needs to be updated to the database as well.
Are there any best practices or patterns surrounding this?
Thanks
It's very simple. Looks like you have a list of items that you need to save. You also need to sort the items by a user-defined order. A user can change order of items, can remove items etc. You want this to be persisted to a database.
Suppose you have a table in a database that stores your items. Just now there is nothing to store a user-defined order. What you need to add is a single property to the type (and a single column to the relation database's table) to reflect an order.
id | operation_name | user_order
--------------------------------
So when user reorders items, you save all of them back to the database, or alternatively execute an UPDATE statement and update the user_order column.
Basically, this is what you have just now. When you query your data, you can use this SQL
Select ...
From ...
Order by user_order
or the same in LINQ
This looks like an audit trailing facility. If those sequences represent some dates then Event sourcing by Martin Fowler could show you some patterns:
Event Sourcing

DataSource WriteXML printing child rows for associate parent

In my database, I have a parent and child tables.
For example, parent table contains last name and address, and child table contains last and first name.
There is a foreign key, so that a row in child table must have corresponding last-name in parent table.
When I read those two tables using 2 DataAdapter's, and add those DataTable's to DataSet, I'd like to print XML, that looks like this:
<parent_table>
<last_name>Smith</last_name>
<address>111 Hi Street, Bye city</address>
<child_table>
<last_name>Smith</last_name>
<first_name>Ann</first_name>
</child_table>
<child_table>
<last_name>Smith</last_name>
<first_name>Bob</first_name>
</child_table>
</parent_table>
However, at present I'm getting two tables printed separately:
<parent_table>
<last_name>Smith</last_name>
<address>111 Hi Street, Bye city</address>
</parent_table>
<child_table>
<last_name>Smith</last_name>
<first_name>Ann</first_name>
</child_table>
<child_table>
<last_name>Smith</last_name>
<first_name>Bob</first_name>
</child_table>
Is there a way to achieve (hopefully using DataSet.WriteXML()) my desired output?
I tried adding ForeignKeyConstraint, and tried adding DataRelation, but neither changed the output.
Disclaimer: the above was hand-written, so please excuse if there's an error in XML. The actual tables contain better foreign key than 'last-name'.
From Writing DataSet Contents as XML Data (ADO.NET):
When writing an XML representation of a DataSet that contains
DataRelation objects, you will most likely want the resulting XML to
have the child rows of each relation nested within their related
parent elements. To accomplish this, set the Nested property of the
DataRelation to true when you add the DataRelation to the DataSet. For
more information, see Nesting DataRelations (ADO.NET).

Moving an entire slice of data to another database using LINQ

I have a database with many tables and constraints (but not much data). The database contains a few separate entities that are bound together by an ID directly or indirectly, as illustrated below:
My target is to move one entire slice of data (including data from all tables in the database) to another physical database in an easy and safe way. It's OK if it doesn't perform very well. In the above example, I would want to move the company with a certain Id as well as all employees of that company and all data related to the employees etc. through all the tables.
I want to do it with a safe compile-checked method, as I want to catch errors whenever I change my database.
The IDs in the database are mostly guids, but there are a few tables using auto incremented IDs.
note
The "Companies" table contains perhaps 5 rows, one for each company. I need to move ONE row from that table, along with all data directly or indirectly related to that row.
Suppose you want to copy data from from a detailsview(tableName=Jobs) to another table(tablename=Company)
string apply = "INSERT INTO Company (JobTitle,CompanyName) select JobTitle,CompanyName from Jobs";
this is just an idea hope it help.
UPDATE :
So this will help you
MSDN - Multiple Bulk Copy Operations (ADO.NET)
With example

Categories