I am creating a report and need to pass a query from SQL Server to Entity Framework.
The query is:
SELECT
contas_receber.id, pessoa.nome, contas_receber.vencimento,
contas_receber.valor_pago, contas_receber.observacao, pessoa.estado
FROM
contas_receber
INNER JOIN
pessoa ON contas_receber.pessoa_id = pessoa.id
INNER JOIN
classificacoes ON classificacoes.id = pessoa.classificacao_id
LEFT OUTER JOIN
servicos ON servicos.id = contas_receber.servico_id
LEFT OUTER JOIN
produto ON produto.id = contas_receber.venda_id
WHERE
(contas_receber.quitado = 0)
AND (CAST(contas_receber.vencimento AS date) >= #datavenc1)
AND (CAST(contas_receber.vencimento AS DATE) <= #datavenc2)
AND (classificacoes.id = #idclassificacao OR #idclassificacao IS NULL)
AND (servicos.id = #idservico OR #idservico IS NULL)
AND (servicos.plano = #tiposervico OR #tiposervico IS NULL)
AND (produto.id = #idproduto OR #idproduto IS NULL)
AND (pessoa.status_financeiro = #statusfinanceiro OR #statusfinanceiro IS NULL)
AND (pessoa.estado = #estado OR #estado IS NULL)
ORDER BY
contas_receber.vencimento
How would you stay in Entity Framework?
what I've done so far:
var contas = from c in _context.ContasReceber
join p in _context.Pessoas on c.PessoaId equals p.Id
join cl in _context.Classificacoes on p.ClassificacaoId equals cl.Id
join ps in _context.PlanosServicos on c.ServicoId equals ps.Id
join pr in _context.Produtos on c.IdVenda equals pr.Id
join tp in _context.TiposProdutos on pr.TiposProdutosId equals tp.Id
where c.Vencimento >= Inicial && c.Vencimento <= Final && (cl.Id == classificacoes || classificacoes is null)
The courses are different because the seats are different, but the sense is the same. My biggest problem is in the conditional whare, when you have to verify that the variable is null
using
Database.SqlQuery():
The Database class represents the underlying database and provides various methods to deal with the database. The Database.SqlQuery() method returns a value of any type.
//Example refer this link
using (var ctx = new SchoolDBEntities())
{
string studentName = ctx.Database.SqlQuery<string>("Select studentname from Student
where studentid=1").FirstOrDefault();
}
Related
I'm trying to convert this SQL Query into Core EF:
SELECT w.IdShippingBatch, w.BookingNumber, COUNT(c.IdShippingOrder) AS ShippingOrders, w.CreatedOn, w.ModifiedOn
FROM dbo.Shipping`enter code here`Batch AS w LEFT OUTER JOIN
dbo.ShippingOrders AS c ON w.IdShippingBatch = c.IdShippingBatch
WHERE (w.IdCompany = 2) AND (w.IdDealer = 1)
GROUP BY w.IdShippingBatch, w.BookingNumber, w.CreatedOn, w.ModifiedOn
I have tried multiple solutions, including several here. My latest attempt looks like:
var data = (from w in _context.ShippingBatch
join c in _context.ShippingOrders on w.IdShippingBatch equals c.IdShippingBatch into t1
where w.IdCompany == idCompany && w.IdDealer == idDealer
from t2 in t1.DefaultIfEmpty()
group t2 by new { w.IdShippingBatch, w.BookingNumber, w.CreatedOn, w.ModifiedOn } into t3
select new ShippingBatchDTO
{
IdShippingBatch = t3.Key.IdShippingBatch,
BookingNumber = t3.Key.BookingNumber,
ShippingOrders = t3.Count(),
CreatedOn = t3.Key.CreatedOn,
ModifiedOn = t3.Key.ModifiedOn
});
I have also tried adding t3.count(m => m.something != null), but that throws an error.
One major point of EF is to map the relationship between entities so that you can leverage LINQ and let EF compose an SQL query rather than trying to replace SQL with LINQ-QL.
If your ShippingBatch is mapped with a collection of ShippingOrders...
var batches = _context.ShippingBatch
.Where(x => x.IdCompany == idCompany && x.IdDealer == idDealer)
.Select(x => new ShippingBatchDTO
{
IdShippingBatch = x.IdShippingBatch,
BookingNumber = x.BookingNumber,
ShippingOrders = x.ShippingOrders.Count(),
CreatedOn = x.CreatedOn,
ModifiedOn = x.ModifiedOn
}).ToList();
If your ShippingBatch does not have a collection of ShippingOrders, but your ShippingOrder reference an optional ShippingBatch.
var batches = _context.ShippingOrder
.Where(x => x.ShippingBatch != null
&& x.ShippingBatch.IdCompany == idCompany
&& x.ShippingBatch.IdDealer == idDealer)
.GroupBy(x => x.ShippingBatch)
.Select(x => new ShippingBatchDTO
{
IdShippingBatch = x.Key.IdShippingBatch,
BookingNumber = x.Key.BookingNumber,
ShippingOrders = x.Count(),
CreatedOn = x.Key.CreatedOn,
ModifiedOn = x.Key.ModifiedOn
}).ToList();
That should hopefully get you moving in the right direction. If not, expand your question to include details of what you are seeing, and what you expect to see along with definitions for the applicable entities.
Please correct the query
IN PL/SQL
SELECT a.MENU_ID, a.menu_label, a.menu_value
FROM tbl_ims_menu a, TBL_IMS_ROLE_ASSIGNED_MENU b,TBL_IMS_USER_ROLE_PRIVILEGES c
WHERE a.menu_id = b.menu_id AND b.urole_id = c.granted_role
AND c.user_id = '3' AND a.menu_master <> '0'
AND a.menu_status = 'Active'
GROUP BY a.menu_id, a.menu_label, a.menu_value
query is working fine there is some issue when rewrite in Entity framework
check the following query
List<TBL_IMS_MENU> listSubMenu = (from m in db.TBL_IMS_MENU
join ra in db.TBL_IMS_ROLE_ASSIGNED_MENU on m.MENU_ID
equals ra.MENU_ID
join rp in db.TBL_IMS_USER_ROLE_PRIVILEGES on ra.UROLE_ID
equals rp.GRANTED_ROLE
where rp.USER_ID == UserID
group m by m.MENU_ID
into g select g).ToList();
if I used Var instead of List then how to fire loop?
I think you need to remove your join statements - and just use the where like you do in raw SQL query:
var qry = (from a in db.TBL_IMS_MENU
from b in db.TBL_IMS_ROLE_ASSIGNED_MENU
from c in db.TBL_IMS_USER_ROLE_PRIVILEGES
where c.USER_ID == UserID
where b.UROLE_ID == c.GRANTED_ROLE
where a.MENU_ID == b.MENU_ID
where a.menu_status == "Active"
where a.menu_master != "0"
select a)
.GroupBy(c => c.menu_id)
.ThenBy(c => c.menu_label)
.ThenBy(c => c.menu_value)
.ToList();
Try something like this:
var listSubMenu = (from m in db.TBL_IMS_MENU
join ra in db.TBL_IMS_ROLE_ASSIGNED_MENU on m.MENU_ID
equals ra.MENU_ID
join rp in db.TBL_IMS_USER_ROLE_PRIVILEGES on ra.UROLE_ID
equals rp.GRANTED_ROLE
where rp.USER_ID == UserID
group m by new { m.MENU_ID, m.menu_label, m.menu_value }
into g select g).ToList();
foreach(var groupItem in listSubMenu)
{
// go through groups like this - groupItem.Key.MENU_ID
foreach(var menuItem in grouItem)
{
//go through each item in group like this - menuItem.GRANTED_ROLE
}
}
i use linq to sql query for retrive records from database.
i use a query,for binding a gridview.
protected void grdBind()
{
try
{
EventManagerDataContext db = new EventManagerDataContext();
var z = (from x in db.EMR_EVENTs
join y in db.EMR_CLIENTs on x.ClientID equals y.ClientID
where y.ClientID==x.EventID
select x.EventID).Count();
var q = from a in db.EMR_CLIENTs
join b in db.EMR_EVENTs on a.ClientID equals b.ClientID
join c in db.EMR_ACCOUNTs on a.ClientID equals c.ClientID
join d in db.EMR_SUBSCRIPTIONs on c.Account_ID equals d.Account_ID
join f in db.EMR_SUBSCRIPTION_KINDs on d.Subscription_kind_ID equals f.Subscription_kind_ID
select new
{
Customer = a.Name,
Events = z,
TurnOver = f.Monthly_Fee,
StartDate = d.Start_Date,
EndDate = d.End_Date,
CreteDate = d.Create_Date,
ClientID = a.ClientID,
EventID = b.EventID,
SubscriptionID = d.Subscription_ID,
Subscription_kind_ID=f.Subscription_kind_ID,
Account_ID=c.Account_ID,
};
grid.DataSource = q.ToList();
grid.PageSize = int.Parse(drpPageSize.SelectedValue);
grid.DataBind();
}
catch
{
throw;
}
}
and i recieve this output for that,
i recieve this output for this query but i don't want this output ,
i want like this output.
clientname events
ketan 18
monika 12
and others records so on means i recieve here client name 9 times and he created events but i want some of events and client name only one time
means i want only one name of client and total number of events,i am new to linq to sql.
so what is the changes in code..?
when you are using join syntax in query you do not need to use 'where'
then change your query to :
var z = (from x in db.EMR_EVENTs
join y in db.EMR_CLIENTs on x.ClientID equals y.ClientID
select x.EventID).Count();
i found solution .here
ans also use with this.useful link
here is my solution.
EventManagerDataContext db = new EventManagerDataContext();
var q = from a in db.EMR_CLIENTs
join b in db.EMR_EVENTs on a.ClientID equals b.ClientID into z
join c in db.EMR_ACCOUNTs on a.ClientID equals c.ClientID
join d in db.EMR_SUBSCRIPTIONs on c.Account_ID equals d.Account_ID
join f in db.EMR_SUBSCRIPTION_KINDs on d.Subscription_kind_ID equals f.Subscription_kind_ID
select new
{
Customer = a.Name,
Events =z.Where(b =>b.ClientID==a.ClientID).Count(),
TurnOver = f.Monthly_Fee,
StartDate = d.Start_Date,
EndDate = d.End_Date,
CreteDate = d.Create_Date,
ClientID = a.ClientID,
SubscriptionID = d.Subscription_ID,
Subscription_kind_ID=f.Subscription_kind_ID,
Account_ID=c.Account_ID,
};
grid.DataSource = q.ToList();
I have some data in an ArrayList and I would like to use that to filter my Linq query using the where clause.
My Linq code below joins two tables and then I filter them using the Where clause. Now I would like to FURTHER filter this query by using the Arraylist as a filter. So the value come from arraylist
I would like the "where" clause to take one more comparison and the value comes from an arraylist:
where rType.Field<string>("ProfSSCMName") == lbProfiles.SelectedValue && rType.Field<string>("Name") == lbHWTypes.SelectedValue && **arrayList.Tostring()**
This is the code that I am using.
Can anyone tell me how can I further filter my Linq query using the values in the arraylist?
joined = from rType in ds.Tables["HWTypes"].AsEnumerable()
join rStock in ds.Tables["Stock"].AsEnumerable()
on rType.Field<string>("ProductID") equals rStock.Field<string>("Partno")
where rType.Field<string>("ProfSSCMName") == lbProfiles.SelectedValue && rType.Field<string>("Name") == lbHWTypes.SelectedValue
select new
{
TagNumber = rStock.Field<string>("TagNumber"),
SerialNumber = rStock.Field<string>("SerialNumber"),
Partno = rStock.Field<string>("Partno"),
PartType = rStock.Field<string>("PartType"),
EcopartSubtype = rStock.Field<string>("EcopartSubtype"),
AzertyQuerty = rStock.Field<string>("Azerty/Querty"),
ProductID = rType.Field<string>("ProductID"),
Name = rType.Field<string>("Name"),
SCCMKeyboard = rType.Field<string>("SCCMKeyboard"),
DisplayName = rType.Field<string>("DisplayName"),
ProfSSCMName = rType.Field<string>("ProfSSCMName"),
TagNameDisplayName = rStock.Field<string>("TagNumber") + " " + rType.Field<string>("DisplayName")
// add the other columns you need here
};
You seem to be using Linq-To-Objects.
So you can just use contains on the arraylist
where rType.Field<string>("ProfSSCMName") == lbProfiles.SelectedValue
&& rType.Field<string>("Name") == lbHWTypes.SelectedValue
&& arrayList.Contains( rType.Field<string>("Name") )
OrdersRepository ordersRepository = new OrdersRepository();
var productInQuery = ordersRepository.ProductIn;
var orderInfoQuery = ordersRepository.OrderInfo;
var result = (from p in productInQuery
from o in orderInfoQuery
where p.refNo == o.refNo
select new {
t1 = p.no,
t2 = o.no}).ToArray();
I wrote like above code for inner join. and I want to add some conditional statement for OrderInfo context.
...
if(orderDate != DateTime.MinValue){
orderInfoQuery = orderInfoQuery.Where(x => x.orderDate == orderDate);
}
// also I tried this too. no is integer in Mysql and I'm sure there is 222 value.
// but still it return always null...
orderInfoQuery = orderInfoQuery.Where(x => x.no == 222);
var result = (from p in productInQuery
from o in orderInfoQuery
where p.refNo == o.refNo
select new {
t1 = p.no,
t2 = o.no}).ToArray();
I add the conditional statement but it return always null. (I check DB(Mysql) value is existed)
And is it not inner join?
var result = (from p in productInQuery
join o in orderInfoQuery on p.refNo equals o.refNo
select new {
t1 = p.no,
t2 = o.no}).ToArray();
Any body know this, please advice me~
Thank you!
[EDIT]
OMG, I'm so sorry, the productIn and orderInfo is never matched. that's why it return always null.
May I ask question again please,
var result = (from p in productInQuery
join o in orderInfoQuery on p.refNo equals o.refNo
join x in productQuery on p.productNo equals x.no
join t in productOutQuery on p.no equals t.productInNo into productIn
from t in productIn.DefaultIfEmpty()
orderby o.processDate descending
select new
{
qty = p.qty,
dateIn = o.processDate,
dateOut = (DateTime?)(from m in orderInfoQuery where m.refNo == t.refNo select m.processDate).FirstOrDefault(),
etaDate = (DateTime?)(from w in orderInfoQuery where w.refNo == t.refNo select w.eta).FirstOrDefault(),
}).ToArray();
this is my linq code, and I want to search by etaDate now.
how can I write linq code. In above code, the etaDate is just sub query. and I want to get
the date that etaDate is exactly matched with specific date.
could you help me please?
Thank you!
You might want to make sure that the types between the two dates here:
orderDate != DateTime.MinValue
and here:
x.orderDate == orderDate
are in the same date format. If one value has a different format or adds extra precision, then the conditional will always fail.