Mapping values in dropdown - c#

Table 1:
ID
1
2
3
4
5
6
7
8
9
0
I have to create dropdowns called MasterID & ChildID out of this ID column. MasterID & ChildID will have ID column values based on mapping. Mapping process will be like any values from the ID column can be MasterID or ChildID. But MasterID will have unique ChildID.
Note: While binding MasterID and ChildID dropdowns it will have ID values on pageload. Once mapping starts then the values should come accordingly. Also I have a Add button in my page and once I select values in two dropdowns and add then they will get saved in the DB.
For ex: If I select the ID value 1 in MasterID and 5 in ChildID and add them to DB, then the next time the value 1 from ID column can come in MasterID dropdown but not in ChildID. Also the value 5 from ID column is mapped to ChildID and saved, where this value should not appear in MasterID and ChildID dropdowns. Once any value is selected as the ChildID and saved, then it shouldn't get displayed inMasterIDor inChildID` dropdowns.
Final Output should be like this.
Table 2:
MasterID ChildID
1 5
2 3
2 6
4 7
4 8
9 0
Can anyone help me or suggest how I can achieve this for asp.net web application.

You can use LINQ to filter the values that you need to display in both the dropdowns.
Something like following.
int[] table1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int[] tableMaster = new int[] { 1, 2, 2, 4, 4, 9 };
int[] tableChild = (from t in table1
where !tableMaster.Contains(t)
select t).ToArray();
Note: This is just an example so that you get an idea.

Related

How to display column data as row header in Crystal Reports

product Table
I have 2 tables, product table with productID and productName :
Product id productName
-----------------------
1 BUR
2 bariis
3 moos
and branch table with branch Id and branchName:
branchid branchName
----------------------
B1 Sb1
B2 Cb2
I want to display a report in Crystal Reports like this
branch BUR bariis moos
--------------------------
Sb1 3 4 4
Cb2 2 1 5
Look at the image for clear understanding
Simply use the option to Insert, CrossTab...

update index after a delete operation

I have a Customer Table that has a CustIndex column. From the UI a User can delete a customer that was for example at Index 3. What I need to do then determine if there was a Customer with an Index at 4 and renumber it to be 3, (so basically keep the sequence).
At the minute I just have some code like below:
private void UpdateCustomerIndexes(IList<CustomerDTO> customers)
{
var customerIndexes = customers.Select(c => new { c.customerId, c.customerIndex });
//unitIndexes.Select(u => u.)
}
I am keeping wanting to keep the CustomerId so if I need to update the DB I can say update customer set index = 3 where customerid = 4 for example.
So if I had 5 customer on the UI and I delete Customer at Index 5
If I set a breakpoint on customerIndexes, the data is:
{ CustomerId = 33, CustomerIndex = 1 },
{ CustomerId = 34, CustomerIndex = 2 },
{ CustomerId = 35, CustomerIndex = 3 },
{ CustomerId = 36, CustomerIndex = 4 }
So, in this case, the CustomerIndex is in sequence so there is nothing to do. However, lets say now I deleted CustomerIndex 2 on the UI. The data would now look like:
{ CustomerId = 33, CustomerIndex = 1 },
{ CustomerId = 35, CustomerIndex = 3 },
{ CustomerId = 36, CustomerIndex = 4 }
Now the Customer Index is out of sequence and I need to renumber CustomerIndex 3 to 2 and CustomerIndex 4 to 3 and then save this to the DB so I can say make the update statements setting the Index to the new value for that CustomerId.
I know my CustomerIndex will never go above 50 so would it be an idea to use something like:
var list = new List<int>(new[] { customerIndexes.Select(c => c.customerIndex) });
var result = Enumerable.Range(0, 50).Except(list);
However, I think this will only tell me if a number in sequence is missing - I am missing how to save which index needs to be updated for which customerid or do nothing in the case when they are in sequence
After you delete the record with CustomerIndex of 2, run the following SQL:
UPDATE TableName SET CustomerIndex = CustomerIndex - 1 WHERE CustomerIndex > 2
This will ensure the CustomerIndex of all 'greater' records are reduced by 1 to compensate.
Similarly, if a customer deletes CustomerIndex of 5, change both references to 2 above to 5.

Linq - select N-level childs from hierarchy parents

I have 2 tables as below:
Parent child relationship (table 1):
SourceId SourceParentId
1 null
2 1
3 2
4 null
Items (Table 2):
Id SourceId
1 1
2 1
3 2
4 2
5 3
6 4
How to write a linq query that return me all items based on Source ID? If my input is SourceId = 1, i will get items 1,2,3,4 & 5. If my input for sourceId is 2, i will get 3 items: 3, 4 & 5. If my input for sourceID is 4, it will return me item 6. My parent child is N-level and items can appear at any level.
Help :(
Here try this
--Variable to hold input value
DECLARE #inputSourceID INT = 1;
--Recursive CTE that finds all children of input SourceID
WITH MyCTE
AS
(
SELECT SourceID,
SourceParentID
FROM table1
WHERE SourceID = #inputSourceID
UNION ALL
SELECT table1.SourceID,
table1.SourceParentID
FROM table1
INNER JOIN MyCTE
ON table1.SourceParentID = MyCTE.SourceID
)
--Join the CTE with the table2 to find all id
SELECT table2.ID
FROM MyCTE
INNER JOIN table2
ON MyCTE.SourceID = table2.SourceID

Datatable - Using linq I want to update a row with the count

I have a C# datatable with:
Id ParentId Name
======== =========== =====
1 A
2 1 b
3 1 c
4 2 d
6 5 e
I want to add a column or just the result that will identify any ParentIds that do not have an Id. Basically in the above table, I want to find Id=6 since ParentId 5 is invalid.
I want to identify anywhere a parentId is not listed as an Id
I thought this could be accomplish with Linq.
This will return all rows where the ParentId does not appear in any row as Id
rows.Where(r1 => !rows.Any(r2 => r1["ParentId"] == r2["Id"]))

LINQ grouping and ignoring column

I'm using EF and LINQ.
I have the following in my db table
branchId Name ItemId CategoryId
2 Test1 1 1
5 Test1 1 1
3 Test1 1 2
2 Test2 2 1
7 Test2 2 1
I need to group by ItemId and BranchId should be ignored, so the output should be
Name ItemId CategoryId
Test1 1 1
Test1 1 2
Test2 2 2
Please help. thanks
You need to apply group by which is on multiple column, so for that you need to go for code like as below which do group by on multiple columns....
var numberGroups = from it in context.items
group it by new { it.ItemId,it.Name,it.CateogryID } into g
select new { ItemID = g.Key.ItemID, Name= g.Key.Name
CategoryID = g.Key.CategoryID };
var query = from item in context.Item
group by item.Name,ItemId,item.CategoryId
select item.Name,Item.Id,item.CategoryId;

Categories