How place where condition in LINQ - sql-server

I am fetching the data based on ID from multiple tables. Here is my code.
public async Task<IActionResult> GeneratePDF(string uniqID)
{
//Other code
List<MasterDetails> MasterData = await _dbInvoiceInfoContext.GetAllInvoiceAsync();
CustData.CustInvoiceInfo = MasterData[0].CustInvoiceInfo;
CustData.CustInvoiceTotalInfo = MasterData[0].CustInvoiceTotalInfo;
List<MasterDetails> MasterData1 = await _dbClientInfoContext.GetAllClientAsync();
CustData.CustClientInfo = MasterData1[0].CustClientInfo;
CustData.CustClientAddress = MasterData1[0].CustClientAddress;
IEnumerable<MasterDetails> clientRecord = from c in CustData.CustInvoiceInfo
join a in CustData.CustInvoiceTotalInfo on
c.InvoiceUniqueID equals a.InvoiceId into table1
from a in table1.ToList()
join b in CustData.CustClientInfo on
c.InvoiceClientId equals b.ClientId into table2
from b in table2.ToList()
select new MasterDetails
{
finalInvoiceInfo = c,
finalInvoiceTotalInfo = a,
finalClientInfo = b
};
}
How can I place the where condition in the above code and fetch the data based on unidID?
The unidID is the ID which I am passing from another ActionResult Page.

You can add Where after table2.ToList(), but please note that parentheses are required to treat the content of the search as a complete object.
IEnumerable<MasterDetails> clientRecord = (from c in CustData.CustInvoiceInfo
join a in CustData.CustInvoiceTotalInfo on
c.InvoiceUniqueID equals a.InvoiceId into table1
from a in table1.ToList()
join b in CustData.CustClientInfo on
c.InvoiceClientId equals b.ClientId into table2
from b in table2.ToList()).Where(a=>a.unidID=unidID)
select new MasterDetails
{
finalInvoiceInfo = c,
finalInvoiceTotalInfo = a,
finalClientInfo = b
};
Or
IEnumerable<MasterDetails> clientRecord = from c in CustData.CustInvoiceInfo
join a in CustData.CustInvoiceTotalInfo on
c.InvoiceUniqueID equals a.InvoiceId into table1
from a in table1.ToList()
join b in CustData.CustClientInfo on
c.InvoiceClientId equals b.ClientId into table2
from b in table2.ToList()
select new MasterDetails
{
finalInvoiceInfo = c,
finalInvoiceTotalInfo = a,
finalClientInfo = b
};
var data=clientRecord.Where()...

Related

No value given for one or more required parameters. at using (OleDbDataReader rdrGLAcounts = cmdGLAcounts.ExecuteReader())

string sql = #"SELECT tblglaccounts.glaccountid as id,tblglaccounts.account as glaccount,
rtrim(tblglaccounts.accountname) as glaccountname, credit.tblgltransactions FROM
tblglaccounts INNER JOIN tblgltransactions ON tblglaccounts.glaccountid = tblgltransactions.gltransactionid
where glaccountstatus = 'True'"+" order by tblglaccounts.account";

How to join multiple columns with only one foreign key using linq

hello stackoverflow community, thats my first question on stack over flow. Thanks for help in advance.
Please help to join 2 tables where one table have list of specialties with thier ids as primary key and second table have 3 columns contains ids of specialties as foreign keys.
that's my code
var q = (from u in db.Providers
join v in db.PracticeSpeciality
on new { p1 = u.prSpecId1, p2 = u.prSpecId2, p3 = u.prSpecId3 }
equals new { p1 = v.pracSpecId, p2 = v.pracSpecId, p3 = v.pracSpecId }
where (u.prId == pc.stprId)
select new
{
u.prFname,
u.prLname,
u.prMI,
u.prTitle,
v.
}).FirstOrDefault();
If you want to return the data of specialists for a provider this will work.
var q = (from prov in providers
join esp1 in praEspec on prov.prSpecId1 equals esp1.pracSpecId
join esp2 in praEspec on prov.prSpecId2 equals esp2.pracSpecId
join esp3 in praEspec on prov.prSpecId3 equals esp3.pracSpecId
where prov.prId == pc.stprId
select new
{
esp1,
esp2,
esp3
}).FirstOrDefault();

display all values of table even without value in sql

I have been working for this for 2 days and i can't combine my tables with 3 different status for example approved, rejected and pending..
In this query it only display all the approved
Select * from
productdesignapproval a,
productinformation b,
department c,
subdivision e,
class f,
subclass g,
productseason h,
year i,
productstylecode j,
productgenericnumber k,
users l
WHERE
a.productinformationID = b.productinformationID AND
b.departmentID = c.departmentID AND
b.subdivisionID = e.subdivisionID AND
b.classID = f.classID AND
b.subclassID = g.subclassID AND
b.productseasonID = h.productseasonID AND
b.yearID = i.yearID AND
a.productinformationID = j.productinformationID AND
a.productinformationID = k.productinformationID AND
l.userID = a.designerID AND a.approvalstatus = 'Approved'
If i changed the a.approvalstatus to Pending there's no value that will appear
THe database works like this if the design is approved there is a specific
productstylecode j, productgenericnumber k, and if not this is my query
Select * from
productdesignapproval a,
productinformation b,
department c,
subdivision e,
class f,
subclass g,
productseason h,
year i,
users l
WHERE
a.productinformationID = b.productinformationID AND
b.departmentID = c.departmentID AND
b.subdivisionID = e.subdivisionID AND
b.classID = f.classID AND
b.subclassID = g.subclassID AND
b.productseasonID = h.productseasonID AND
b.yearID = i.yearID AND
l.userID = a.designerID AND
a.approvalstatus != 'Approved'
if the value is pending/rejected there's no productstylecode and genericnumber.
Question how can i combine the 2 tables into 1 table? so i can display all approved with product style code and generic number and all pending and rejected without product style code and generic number. Thanks!
It seems you need something like this query
SELECT * FROM
productdesignapproval a
JOIN productinformation b ON a.productinformationID = b.productinformationID
JOIN department c ON b.departmentID = c.departmentID
JOIN subdivision e ON b.subdivisionID = e.subdivisionID
JOIN class f ON b.classID = f.classID
JOIN subclass g ON b.subclassID = g.subclassID
JOIN productseason h ON b.productseasonID = h.productseasonID
JOIN year i ON b.yearID = i.yearID
JOIN users l ON l.userID = a.designerID
LEFT JOIN productstylecode j ON a.productinformationID = j.productinformationID AND a.approvalstatus = 'Approved'
LEFT JOIN productgenericnumber k ON a.productinformationID = k.productinformationID AND a.approvalstatus = 'Approved'

Query conversion from sybase to SQL Server

I have one query that is to convert from sybase to SQL server, I think we just need to change join operations.. Here is part of the query:
Can someone help me here?
FROM sy_trcr_divided d, fd_income_trans t
WHERE d.fd_id_income is not null
AND t.fd_income_transfer_id = d.fd_proc_id
AND t.fd_income_est_yn = 'Y'
AND d.fd_id_income = a.fd_id
AND d.fd_dist_to_i_or_p = 'A'
AND d.gl_year = :p_lYear
AND d.gl_period = :p_lPeriod
AND d.fd_include_stip_yn = 'Y'),0) AS ADDTO_FUND
FROM sy_est_income_detail a,
fd_master
WHERE a.fd_id *= fd_master.fd_id and
a.hr_id = :p_szHRID
My thought: have to replace *= with a join.
The *= should be a left join and the piece of the query you provided should probably be:
FROM sy_trcr_divided d
INNER JOIN fd_income_trans t ON t.fd_income_transfer_id = d.fd_proc_id
WHERE d.fd_id_income is not null
AND t.fd_income_est_yn = 'Y'
AND d.fd_id_income = a.fd_id
AND d.fd_dist_to_i_or_p = 'A'
AND d.gl_year = :p_lYear
AND d.gl_period = :p_lPeriod
AND d.fd_include_stip_yn = 'Y'),0) AS ADDTO_FUND
FROM sy_est_income_detail a
LEFT JOIN fd_master ON a.fd_id = fd_master.fd_id
WHERE a.hr_id = :p_szHRID

Get results from multiple tables after grouping using Linq-to-SQL

I think the answer should be easy, but I'm just struggling:
Would like to have data from 2 tables in a LINQ query similar to:
from f in Faults
join af in AvailabilityFaults on f.FaultID equals af.FaultID
join a in Availabilities on new { af.CalendarDay, af.CircuitNumber}
equals new { a.CalendarDay, a.CircuitNumber}
join e in ExternalImportAvailabilities on new { a.CalendarDay, a.CircuitNumber }
equals new { e.CalendarDay, e.CircuitNumber }
where a.CalendarDay.Value.Day != 1
group f by f.FaultID into groupF
select new {groupF, e.CalendarDay}
The problem here comes in that it can't find e.CalendarDay in the select clause.
I also tried things like: CalendarDay= e.Max(e=>e.CalendarDay), but e is not in the current context.
How can I add data from table 'e' in the select clause?
If I understood you properly, this is what you are looking for:
from f in Faults
join af in AvailabilityFaults on f.FaultID equals af.FaultID
join a in Availabilities on af.CalendarDay equals a.CalendarDay and af.CircuitNumber equals a.CircuitNumber
join e in ExternalImportAvailabilities on a.CalendarDay equals e.CalendarDay and a.CircuitNumber equals e.CircuitNumber
where a.CalendarDay.Value.Day != 1
group new {f, e} by f.FaultID into groupFE
select new {GroupF = groupFE.Select( gfe => gfe.f), CalendarDay = groupFE.Max( gfe => gfe.e.CalendarDay)}
If you want e.CalendarDay in your select, then you normally would include it in the group, as in
group f by new { f.FaultID , e.CalendarDay } into groupF
select new {groupF.Key.FaultID, groupF.Key.CalendarDay}

Resources