Notify in .NET application if SQL data has changed - c#

I have got two tables. Table A contains the main data in every change state, Table B has the detail data.
Table A
| ID | Name |
| 1 | House |
| 2 | Tree |
| 3 | Car |
Table B
| ID | FK | DateTime | Color | Type |
| 1 | 1 | 2017-26-01T13:00:00 | Red | Bungalow |
| 2 | 2 | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | 1 | 2017-26-01T13:10:00 | Green | Bungalow |
| 4 | 3 | 2017-26-01T13:15:00 | Yellow | Smart |
| 5 | 1 | 2017-26-01T13:20:00 | White | Bungalow |
| 6 | 3 | 2017-26-01T13:25:00 | Black | Smart |
Result to watch
| ID | Name | DateTime | Color | Type |
| 1 | House | 2017-26-01T13:20:00 | White | Bungalow |
| 2 | Tree | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | Car | 2017-26-01T13:25:00 | Black | Smart |
The current state of an entity in Table A is described by the record with the youngest timestamp in Table B.
Now, I want to be nofitied, if an entity gets a new state (new record in Table B) or a new entity is created (new records in Table A and B), i.e. it could look like.
New result to watch
| ID | Name | DateTime | Color | Type |
| 1 | House | 2017-26-01T13:20:00 | White | Bungalow |
| 2 | Tree | 2017-26-01T13:05:00 | Brown | Oak |
| 3 | Car | 2017-26-01T19:25:00 | Silver | Smart |
| 4 | Dog | 2017-26-01T20:25:00 | White / Black | Poodle |
With SqlDependency it is not possible to be notified for a statement which contains GROUP BY with MAX aggregate, window functions or TOP clause. So I have no idea how I can get the last detail data for an entity.
Is there any possibility to create a statement for this requirement or are there any other ways to be notified after changes for this result?

You can create and use SQL Triggers usin CLR
Here - SQL Trigger CLR

Related

How to list the products under each category in sql? side by side

I'm a bit of a beginner in sql and I need your help.
I'm sorry that I don't know if the question is correct.
now the code gives this;
+---------+----------+-------------+------------+
| id | CompanyId| DealId | price |
+---------+----------+-------------+------------+
| 1 | 1 | 1 | 100 |
| 2 | 1 | 2 | 50 |
| 3 | 1 | 3 | 25 |
| 4 | 2 | 1 | 1000 |
| 5 | 2 | 2 | 2000 |
| 6 | 2 | 3 | 2500 |
+---------+----------+-------------+------------+
but this is what i want;
+---------+----------+-------------+------------+------------+--+
| id | companyId| DealName1 | DealName2 | DealName3 | |
+---------+----------+-------------+------------+------------+--+
| 1 | 1 | 100 | 50 | 25 | |
| 2 | 2 | 1000 | 2000 | 2500 | |
| 3 | 3 | value | value | value | |
| 4 | 4 | value | value | value | |
+---------+----------+-------------+------------+------------+--+
select CompanyId
,[1] as DealName1
,[2] as DealName2
,[3] as DealName3
from (select CompanyId, DealId, price from t) t
pivot (sum(price) for DealId in([1],[2],[3])) p
CompanyId
DealName1
DealName2
DealName3
1
100
50
25
2
1000
2000
2500
Fiddle

GroupBy and Select in EF Core

I'm trying to get a list of all unique combinations of make and model and also include the most recent time stamp and the color from the following table:
+-------+---------+-------------------------+--------+
| Make | Model | Last Seen | Color |
+-------+---------+-------------------------+--------+
| Volvo | XC90 | 2022-03-11 13:30:31.623 | Blue |
| Volvo | XC90 | 2022-03-11 14:30:31.623 | Green |
| BMW | M3 | 2022-03-11 15:30:31.623 | Orange |
| Ford | Mustang | 2022-03-11 16:30:31.623 | Red |
+-------+---------+-------------------------+--------+
This query will give me a list of all combinations of make and model and include the latest timestamp, but I don't know how I can also include the Color.
var lastSeen = _context.Cars.GroupBy(o => new {o.Make, o.Model})
.Select(n => new {Make = n.Key.Make, Model = n.Key.Model, Max = n.Max(a => a.LastSeen)})
.ToList();
I'm looking for the following output:
+-------+---------+-------------------------+--------+
| Make | Model | Last Seen | Color |
+-------+---------+-------------------------+--------+
| Volvo | XC90 | 2022-03-11 14:30:31.623 | Green |
| BMW | M3 | 2022-03-11 15:30:31.623 | Orange |
| Ford | Mustang | 2022-03-11 16:30:31.623 | Red |
+-------+---------+-------------------------+--------+
Thanks!

WPF Datagrid or maybe something else that can have indents in the first column?

I have a table with a bunch of data this is a small sample:
Header - Object name
Waarde
ExportRoot.a80_02_00_01_Status.Settings.Off
0
ExportRoot.a80_02_00_01_Status.Standby
0
ExportRoot.a80_02_00_01_Status.Cooling
1
ExportRoot.a80_02_00_01_Status.Drying
0
ExportRoot.a80_02_00_01_Status.Heating
0
ExportRoot.a80_02_00_01_Status.PrepDrain
0
I wish to style this table in a way to where its more readable by removing the object parts and indenting the table like how it looks below.
| Header - Object name | Waarde |
|--------------------------|---------|
| ExportRoot | |
| | a80_02_00_01_Status | |
| | | Settings | |
| | | | Off | 0 |
| | | | Standby | 0 |
| | | | Cooling | 1 |
| | | | Drying | 0 |
| | | | Heating | 0 |
| | | | PrepDrain | 0 |
Is there a way of doing this and if so what should I be looking for all I've found so far are tables with sub tables and that not exactly what I'm looking for. If anyone has an idea let me know I'm super lost.
So basically you need a TreeListView:
There is one project on CodeProject that you can check:
WPF TreeListView Control
There is also commercial products, like DevExpress:
DevExpress TreeList View

Aggregate multiple columns in DataTable and put result to new DataTable

Hi i have a DataTable with following data formating. I am trying to aggregate in C# on condition. I am a Newbie to Linq.
Profile | Nr | Result|
------------------------
A | 1 | OK |
A | 2 | NOK |
A | 2 | OK |
A | 3 | OK |
A | 4 | OK |
A | 5 | OK |
BB | 1 | OK |
BB | 2 | NOK |
BB | 2 | NOK |
BB | 2 | OK |
BB | 3 | OK |
BB | 4 | OK |
BB | 5 | OK |
I Want to aggregate by first columnt (Profile) and count rows where the Result is OK.
The expected result should be a new DataTable and should look like this.
Profile | Count|
----------------
A | 5 |
BB | 5 |
How can i do this in C# ?

merge single table column with different types

Table Columns
or_cv_no
here is where 'cv_no' and 'or_no' stored
type
different types are 'CV' and 'OR'
amount
here is where 'cv_amt' and 'or_amt' stored
date
data that is being fetched has same date
my query is this
SELECT IIF(type = 'CV', or_cv_no) AS cv_no, IIF(type = 'CV', amount) AS cv_amt, IIF(type = 'OR', or_cv_no) AS or_no, IIF(type = 'OR', amount) AS or_amt FROM transacAmt
and the output is
how can i output it to look like this sample:
| cv_no | cv_amt | or_no | or_amt |
|---------------------------------|
| 1234 | 1,500 | 4321 | 1,200 |
|-------+--------+-------+--------|
| 7777 | 1,700 | 2222 | 1,760 |
|-------+--------+-------+--------|
| 1111 | 1,900 | 3333 | 1,530 |
|-------+--------+-------+--------|
| | | 5355 | 1,420 |
|-------+--------+-------+--------|
| | | 1355 | 4,566 |
EDIT: The output must be like this: marked 'x' must be removed.. or be filled by data

Categories