Linq query with List<T> - c#

I have list List<Employee> it contains data like
Name Year Salary
John 2001 10000
Warn 2001 11000
George 2001 12000
Nick 2001 13000
Warn 2002 14000
John 2002 15000
Adam 2002 16000
Kile 2003 17000
John 2003 18000
Kile 2004 19000
Warn 2004 20000
I need to convert like that
Year John Warn George Nick Adam Kile
----------------------------------------------------------
2001 10000 11000 12000 13000 0 0
2002 15000 14000 0 0 16000 0
2003 18000 0 0 0 0 17000
2004 0 20000 0 0 0 19000
Is it possible with LINQ?

Would GroupBy() be sufficient?
employees.GroupBy(x => x.Year);

var annualGroups =
from e in List<Employee>
group e by e.Year into y
select new { Year = y.Key, Employees = y };

Related

Group by an array of objects and select based on multiple conditions

I have an array of objects that may contain duplicate records based on some ID.
I am trying to group the objects by ID and from each group pick the record where either status is 3 or with the earliest date (status takes precedence).
ID Status Last LastModified
1 1 Smith 02/06/2023 04:00 AM
1 2 Smith 02/06/2023 02:00 AM
2 1 Jones 02/01/2023 11:24 AM
3 1 Jack 02/05/2023 02:00 AM
3 3 Jack 02/06/2023 06:00 AM
What I would want from a query is to get the following:
ID Status Last LastModified
1 2 Smith 02/06/2023 02:00 AM
2 1 Jones 02/01/2023 11:24 AM
3 3 Jack 02/06/2023 06:00 AM
I get the records based on date but not sure how to stick the status portion in there:
var filtered = from d in tests.AsEnumerable()
group d by d.ID into g
select g.OrderBy(p => p.DateTime).FirstOrDefault();
This returns:
ID Status Last LastModified
1 2 Smith 02/06/2023 02:00 AM
2 1 Jones 02/01/2023 11:24 AM
3 1 Jack 02/05/2023 02:00 AM
How about:
var filtered =
from d in tests.AsEnumerable()
group d by d.ID into g
select g.OrderByDescending(p => p.Status == 3).ThenBy(p => p.DateTime).FirstOrDefault();

How to make a sum of total for each id and grand total

Table 1
id name
001 raja
002 vijay
003 suresh
Table 2
id value
001 100
001 200
001 150
002 200
003 150
003 200
and i want the out put like this
id name value
001 raja 450
002 vijay 200
003 suresh 350
................
1000 (sum of value)
using crystal report
Select table2.id,table1.name,
sum(table2.value) from table2,table1
where table2.id= (table1.id)
group by table2.id
union
select '----','-----',sum(value) from table2
;
This will give you the sum of values of each specific group id with name

How to remove records from datatable with condition?

I have a datatable like below,
ID--- Month---- Salary
10 Mar 15000
10 Apr 15000
11 Mar 10000
11 Apr 25000
12 Mar 15000
How to make this table to
ID-----Month-----Salary
11 Mar 10000
11 Apr 25000
12 Mar 15000
select distinct salary group by employee id

Oracle timeframe conversion

I have the following configuration: a table called source (bid, valid_from, valid_to, qty) and destination (bid, jan, feb, mar, apr, ...., dec).
I would like to insert the data from source into origin, (spliting the qty equally to the valid_from-valid_to months and the remaining rest to the last month in the timeframe) like this : if the record in source is
00001 01.02.2001 31.06.2001 132
this would be translated into destination :
00001 0 26 26 26 26 28 0 0 0 0 0 0
How could I do this?
Thank you!
I'm assuming valid_from/valid_to never spans multiple years; i.e., TO_DATE(valid_from,'YYYY') always = TO_DATE(valid_to,'YYYY').
SQL> CREATE TABLE source (
2 bid VARCHAR2(5)
3 , valid_from DATE
4 , valid_to DATE
5 , qty NUMBER
6 );
Table created.
SQL> INSERT INTO source VALUES ('00001',TO_DATE('20010201','YYYYMMDD'),TO_DATE('20010630','YYYYMMDD'),132);
1 row created.
SQL> INSERT INTO source VALUES ('00002',TO_DATE('20020301','YYYYMMDD'),TO_DATE('20021231','YYYYMMDD'),59);
1 row created.
SQL> CREATE TABLE destination (
2 bid VARCHAR2(5)
3 , jan NUMBER
4 , feb NUMBER
5 , mar NUMBER
6 , apr NUMBER
7 , may NUMBER
8 , jun NUMBER
9 , jul NUMBER
10 , aug NUMBER
11 , sep NUMBER
12 , oct NUMBER
13 , nov NUMBER
14 , dec NUMBER
15 );
Table created.
SQL> COLUMN jan FORMAT 999
SQL> COLUMN feb FORMAT 999
SQL> COLUMN mar FORMAT 999
SQL> COLUMN apr FORMAT 999
SQL> COLUMN may FORMAT 999
SQL> COLUMN jun FORMAT 999
SQL> COLUMN jul FORMAT 999
SQL> COLUMN aug FORMAT 999
SQL> COLUMN sep FORMAT 999
SQL> COLUMN oct FORMAT 999
SQL> COLUMN nov FORMAT 999
SQL> COLUMN dec FORMAT 999
SQL> INSERT INTO destination
2 SELECT bid
3 , NVL(MAX(DECODE(r,01,split_qty)),0) jan
4 , NVL(MAX(DECODE(r,02,split_qty)),0) feb
5 , NVL(MAX(DECODE(r,03,split_qty)),0) mar
6 , NVL(MAX(DECODE(r,04,split_qty)),0) apr
7 , NVL(MAX(DECODE(r,05,split_qty)),0) may
8 , NVL(MAX(DECODE(r,06,split_qty)),0) jun
9 , NVL(MAX(DECODE(r,07,split_qty)),0) jul
10 , NVL(MAX(DECODE(r,08,split_qty)),0) aug
11 , NVL(MAX(DECODE(r,09,split_qty)),0) sep
12 , NVL(MAX(DECODE(r,10,split_qty)),0) oct
13 , NVL(MAX(DECODE(r,11,split_qty)),0) nov
14 , NVL(MAX(DECODE(r,12,split_qty)),0) dec
15 FROM
16 (
17 SELECT x.bid
18 , x.month_abbr
19 , x.r
20 , x.rn
21 , x.total_months
22 , x.qty
23 , FLOOR(x.qty / x.total_months)
24 + DECODE(x.rn
25 , x.total_months, MOD(x.qty, x.total_months)
26 , 0) split_qty
27 FROM (SELECT s.bid
28 , months.r
29 , ROW_NUMBER()
30 OVER (PARTITION BY s.bid
31 ORDER BY months.r) rn
32 , COUNT(*)
33 OVER (PARTITION BY s.bid) total_months
34 , s.qty
35 FROM (SELECT ROWNUM r
36 FROM DUAL
37 CONNECT BY LEVEL <= 12) months
38 , source s
39 WHERE TO_CHAR(s.valid_from,'YYYY') = TO_CHAR(s.valid_to,'YYYY')
40 AND months.r BETWEEN TO_NUMBER(TO_CHAR(s.valid_from,'MM'))
41 AND TO_NUMBER(TO_CHAR(s.valid_to,'MM'))) x
42 )
43 GROUP BY bid
44 ;
2 rows created.
SQL> SELECT *
2 FROM destination
3 ;
BID JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
----- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
00001 0 26 26 26 26 28 0 0 0 0 0 0
00002 0 0 5 5 5 5 5 5 5 5 5 14
SQL>

How select in multitable a single column where the price is greater than "0"?

i have 5 tables how follow below :
City_TBL
CityCode
ABB
DET
FRI
ROM
Hotel_TBL
HotelCode CityCode (FK) Price
1 ABB 0
2 ABB 10
3 FRI 0
4 DET 0
5 ROM 19
HotelRoom_TBL
RoomID HotelCode (FK) RoomName Price
1 1 Superior 3
2 2 DeLuxe 6
3 1 Panoramic 0
4 3 Suite 0
5 4 Presidential 1
Transfer_TBL
TransferCode CityCode (FK) Price
1 ABB 3
2 ABB 6
3 DET 0
4 FRI 0
Cruise_TBL
CruiseCode CityCode (FK) Price
1 ABB 3
2 DET 0
3 FRI 0
4 ROM 0
I want write down a query(linq) which will return a list of CityCode without duplicate record(CityCode) in which has at least a record where the field/Column "Price" (in the Table Hotel_TBL , Transfer_TBL , Cruise_TBL and HotelRoom_TBL) is greater then '0' as follow result as show below :
Result_TBL
ABB
ROM
How i can do it with linq to sql ?
Thanks so much for your attention.
Have a good time.
Cheers
Sorry i modified the question 'cos i forgot to write down another table(HotelRoom_TBL) so please forgive me this mistake .
Thank so much
Perhaps
var data =
ctx.HotelTbl.Where(row => row.Price > 0).Select(row => row.CityCode)
.Union(
ctx.TransferTbl.Where(row => row.Price > 0).Select(row => row.CityCode)
).Union(
ctx.ResultTbl.Where(row => row.Price > 0).Select(row => row.CityCode)
);
Personally I'd be tempted to use ExecuteQuery though:
var data = ctx.ExecuteQuery<string>(#"
select CityCode from Hotel_Tbl where Price > 0
union select CityCode from Transfer_Tbl where Price > 0
union select CityCode from Result_Tbl where Price > 0").ToList();

Categories

Resources