Hi i'm trying to convert this SQL script in Linq expression
but i donĀ“t know how do the MAX method in Linq
someone can help me?
thank you!
SELECT c.Nome,
c.NumeroRG,
f.Tipo,
f.Descricao,
f.DataHora,
f.IdCliente,
c.IdCliente,
f.IdFrequencia
FROM Cliente c, Frequencia f
WHERE f.Tipo = 1
AND c.IdCliente = f.IdCliente
AND cast(f.DataHora as date) = cast(getdate() as date)
AND f.IdFrequencia = (select MAX(fr.IdFrequencia)
from frequencia fr
where fr.IdCliente =c.IdCliente)
Perhaps something like this:
var query = from client in db.Cliente
join freq in db.Frequencia
on client.IdCliente equals freq.IdCliente
where freq.Tipo == 1
&& freq.DataHora.Date == DateTime.Now.Date
&& freq.IdFrequencia == db.Frequencia.Where(f => f.IdCliente == client.IdCliente)
Max(f => f.IdFrequencia)
select new { .... };
Maybe you need to replace DateTime.Now.Date/DateTime.Today with SqlFunctions.DatePart if you use LINQ-To-Entities, but you haven't mentioned that.
this worked well! thanks
var query = from client in db.Cliente
join freq in db.Frequencia
on client.IdCliente equals freq.IdCliente
where freq.Tipo == true
&& freq.DataHora.Value.Date == DateTime.Today.Date
&& freq.IdFrequencia == db.Frequencia.Where(f => f.IdCliente == client.IdCliente).Max(f => f.IdFrequencia)
select new { Nome = client.Nome, Descricao = freq.Descricao };
Related
I need to get one value of the q2.RoomId in this linq query as below and pass it to string variable . and is it possible to set alias column name for select ?
var result = from q1 in _db.DormApplications
join q2 in _db.DormRooms
on q1.DormRoomId equals q2.Id
where q1.Poster == username && q1.Review == EnableType.YES
&& now > q1.Sdate && now <q1.Edate
select new { q1.Name, q1.Sdate, q1.Edate, q1.DormRoomId,
q2.RoomId };
I tried this to get q2.RoomId ,
String room = result[4].ToString();
but still cannot work ,please anyone can help me ?
If you know the query will only return one row you can write:
var result = (from q1 in _db.DormApplications
join q2 in _db.DormRooms
on q1.DormRoomId equals q2.Id
where
q1.Poster == username && q1.Review == EnableType.YES
&& now > q1.Sdate && now <q1.Edate
select
new {
q1.Name,
q1.Sdate,
q1.Edate,
q1.DormRoomId,
q2.RoomId
}).FirstOrDefault();
var roomId = result.RoomId;
var Name = result.Name;
I'm new in Linq, and I want to convert this sql Query to Linq Format.
This is the SQL format
select *
from investorwallets iw
where transactionid in
(select investordepositid from investordeposits)
or transactionid in
(select withdrawalid from investorwithdrawals)
or transactionid in
(select paymentdistributionid from paymentdistributions)
I've looked on this SO Question too, but no luck for me
EDIT
This is what I have tried. I use Linqpad for testing it
from iw in Investorwallets
where (
from id in Investordeposits // I got error from here
select id.investordepositid
)
Anyone can help me?
Thank you
The most direct is:
from iw in investorwallets
where investordeposits.Any(iten => item.investordepositid == iw.transactionid) ||
investorwithdrawals.Any(iten => item.withdrawalid == iw.transactionid) ||
paymentdistributions.Any(item => item.paymentdistributionid == iw.transactionid)
select iw;
However you can also union the results and then do .Contains:
var ids = investorwithdrawals.Select(item => item.investordepositid)
.Union(investorwithdrawals.Select(item => item.withdrawalid))
.Union(paymentdistributions.Select(item => item.paymentdistributionid));
var result = investorwallets.Where(item => ids.Contains(item.transactionid));
List<investorwallet> investorwallets = GetInvestorwallets();
List<investordeposit> investordeposits = GetInvestordeposits();
List<investorwithdrawal> investorwithdrawals = GetInvestorwithdrawals();
List<paymentdistribution> paymentdistributions = GetPaymentdistribution();
List<investorwallet> newList = investorwallets.Where(x => investordeposits.Any(y=>y.investordepositid == x.transactionid)
|| investorwithdrawals.Any(y => y.withdrawalid == x.transactionid)
|| paymentdistributions.Any(y => y.paymentdistributionid == x.transactionid)).ToList();
I have a query like this:
var rrx = (from camp in db.Campus
join camproom in db.CampusRooms
on camp.Id equals camproom.CampusId
where (camproom.CampusId == 1) && (camp.BranchId == 10) && (camproom.Status == 0)
select new CampusCampusRoom { CampName = camp.Name, CampusRoomNo = camproom.RoomNo, CampusClassCapacity = camproom.ClassCapacity, CampusExamCapacity = camproom.ExamCapacity }).ToList();
How can I perform this query using .include(), .where(), .select() clauses?
Here is an easy to use filtered projection example. This should provide you with a good example of what you want to do with your query.:
var customersWithRecentReservations =
from c in context.Contacts.OfType<Customer>()
where c.FirstName == p_firstName && c.LastName == p_lastName
select new {Customer = c, Reservations = c.Reservations.Where(r => r.ReservationDate >= p_reservationDate)};
var customers = customersWithRecentReservations.AsEnumerable().Select(p => p.Customer);
Try and adapt this code to your situation or let me know if you need further assistance with it ;)
I have this linq query:
var sku = (from a in con.MagentoStockBalances
join b in con.MFGParts on a.SKU equals b.mfgPartKey
join c in con.DCInventory_Currents on b.mfgPartKey equals c.mfgPartKey
where a.SKU != 0 && c.dcKey ==6
select new
{
Part_Number = b.mfgPartNumber,
Stock = a.stockBalance,
Recomended = a.RecomendedStock,
Cato = c.totalOnHandQuantity
}).ToList();
Now i need to remove the c.dcKey ==6 condition and have something like this:
var sku = (from a in con.MagentoStockBalances
join b in con.MFGParts on a.SKU equals b.mfgPartKey
join c in con.DCInventory_Currents on b.mfgPartKey equals c.mfgPartKey
where a.SKU != 0
select new
{
Part_Number = b.mfgPartNumber,
Stock = a.stockBalance,
Recomended = a.RecomendedStock,
Cato = c.totalOnHandQuantity where c.dcKey == 6,
Kerry = c.totalOnHandQuantity where c.dcKey == 7
}).ToList();
Something like this:
Cato = c.dcKey == 6 ? c.totalOnHandQuantity : 0,
Kerry = c.dcKey == 7 ? c.totalOnHandQuantity : 0
The ?: syntax is called a conditional operator.
Instead of adding another join, I would use a separate query in a let:
from a in con.MagentoStockBalances
join b in con.MFGParts on a.SKU equals b.mfgPartKey
where a.SKU != 0
let cs = con.DCInventory_Currents.Where(c => b.mfgPartKey == c.mfgPartKey)
select new
{
Part_Number = b.mfgPartNumber,
Stock = a.stockBalance,
Recomended = a.RecomendedStock,
Cato = cs.Single(c => c.dcKey == 6).totalOnHandQuantity
Kerry = cs.Single(c => c.dcKey == 7).totalOnHandQuantity
}
Though I have no idea how well will LINQ to SQL handle a query like this (if it handles it at all).
var query= from x in context.a
where String.IsNullOrEmpty(param1) || (x.p == param1 && x.i == param2)
select x;
I'm trying to translate this SQL sentence to LINQ but I'm very new in LINQ
select professor.nom, professor.llinatge,
SUM(case when falta.aprovada = 1 then 1 else 0 end) as FJ,
SUM(case when falta.aprovada = 0 then 1 else 0 end) as FNJ
from falta inner join professor on falta.id_profe = professor.id_profe
group by professor.llinatge, professor.nom
I can't get work this in LINQ with JOINs. My best aproach to this in LINQ was:
var query = from f in db.falta
join p in db.professor
on f.id_profe equals p.id_profe
group f by new {p.nom, p.llinatge, f.aprovada} into g
select new
{
nombre = g.Key.nom + " "+ g.Key.llinatge,
fj = g.Select(s=> s.aprovada == true).Count(),
fnj = g.Select(s=> s.aprovada == false).Count()
};
Thank you!
You may try SQL to LINQ ... and over the years I always found Linq Pad as a very handy tool...
Finally I found the LINQ query that match my SQL statement:
from falta in db.Falta
join professor in db.Professor on falta.Id_profe equals professor.Id_profe
group new {professor, falta} by new {
professor.Llinatge,
professor.Nom
} into g
select new {
g.Key.Nom,
g.Key.Llinatge,
FJ = (System.Int64?)g.Sum(p => (
p.falta.Aprovada == true ? 1 : 0)),
FNJ = (System.Int64?)g.Sum(p => (
p.falta.Aprovada == false ? 1 : 0))
};
Thank you for your help! Hope this helps!