I work on entity framework core 7 . I face issue I can't do or use union on entity framework core instead of use union on sql server
so i have view contain or return collect of unions as below
SQL
CREATE VIEW [dbo].[vDsales]
AS
SELECT sldate, id, sdate, till, cashier, trnno, brcode, itemno, qty, price, amt, itemflag, stime
FROM NewSaleNAV.dbo.dsales WITH (NOLOCK)
WHERE sldate >= '2023-01-01'
UNION
SELECT sldate, id, sdate, till, cashier, trnno, brcode, itemno, qty, price, amt, itemflag
FROM NewSaleNAV01.dbo.dsales WITH (NOLOCK)
WHERE sldate >= '2023-01-01'
UNION
SELECT sldate, id, sdate, till, cashier, trnno, brcode, itemno, qty
FROM NewSaleNAV_Arc22.dbo.dsales WITH (NOLOCK)
WHERE sldate >= '2022-01-01'
UNION
SELECT sldate, id, sdate, till, cashier, trnno, brcode, itemno, qty
FROM NewSaleNAV01_Arc22.dbo.dsales WITH (NOLOCK)
WHERE sldate >= '2022-01-01'How to convert multi statement union to entity framework core by write statement above with LINQ to sql
my class will return data as below
C#
public class vDsales
{
public DateTime sldate { get; set; }
public int id { get; set; }
public DateTime sdate { get; set; }
public int till { get; set; }
public int cashier { get; set; }
public int trnno { get; set; }
public int brcode { get; set; }
public int itemno { get; set; }
public int qty { get; set; }
public decimal price { get; set; }
public decimal amt { get; set; }
public int itemflag { get; set; }
public int stime { get; set; }
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Jul 18, 2023, 11:56 PM EDT
Here's an example of how you can convert your SQL UNION statement to LINQ in Entity Framework Core:
DateTime startDate = new DateTime(2023, 01, 01); DateTime archiveStartDate = new DateTime(2022, 01, 01); var query = dbContext.NewSaleNAV .Where(s => s.sldate >= startDate) .Select(s => new vDsales { sldate = s.sldate, id = s.id, sdate = s.sdate, till = s.till, cashier = s.cashier, trnno = s.trnno, brcode = s.brcode, itemno = s.itemno, qty = s.qty, price = s.price, amt = s.amt, itemflag = s.itemflag, stime = s.stime }) .Concat(dbContext.NewSaleNAV01 .Where(s => s.sldate >= startDate) .Select(s => new vDsales { sldate = s.sldate, id = s.id, sdate = s.sdate, till = s.till, cashier = s.cashier, trnno = s.trnno, brcode = s.brcode, itemno = s.itemno, qty = s.qty, price = s.price, amt = s.amt, itemflag = s.itemflag, stime = 0 // or any appropriate value for this table })) .Concat(dbContext.NewSaleNAV_Arc22 .Where(s => s.sldate >= archiveStartDate) .Select(s => new vDsales { sldate = s.sldate, id = s.id, sdate = s.sdate, till = s.till, cashier = s.cashier, trnno = s.trnno, brcode = s.brcode, itemno = s.itemno, qty = s.qty, price = 0, // or any appropriate value for this table amt = 0, // or any appropriate value for this table itemflag = 0, // or any appropriate value for this table stime = 0 // or any appropriate value for this table })) .Concat(dbContext.NewSaleNAV01_Arc22 .Where(s => s.sldate >= archiveStartDate) .Select(s => new vDsales { sldate = s.sldate, id = s.id, sdate = s.sdate, till = s.till, cashier = s.cashier, trnno = s.trnno, brcode = s.brcode, itemno = s.itemno, qty = s.qty, price = 0, // or any appropriate value for this table amt = 0, // or any appropriate value for this table itemflag = 0, // or any appropriate value for this table stime = 0 // or any appropriate value for this table })); var result = query.ToList();Thanks