i used below linq but it is giving error on first "join" keyword
(from st in queryResult.ToList()
join rf in rainfall.ToList() on Convert.ToInt32(st.StationID) equals Convert.ToInt32(rf.StationID)
join wl in water on
new { StationID = Convert.ToInt32(st.StationID), DataRecieved = rf.DataRecieved }
equals
new { StationID = Convert.ToInt32(wl.StationId), DataRecieved = wl.DataRecieved } into gj
from subpet in gj.DefaultEmpty()
select new
{
Stationname = st.Stationname,
Stationid = st.stationid,
State = st.state,
Datarecieved = rf.datarecieved,
dailyrainfall = rf.dailyrainfall,
cumlativerainfall = rf.cumrainfall,
waterlevel1 = subpet.waterlevel1
})
It is giving error "Does not contain a definition of "DefaultEmpty" and no extension method "DefaultEmpty" accepting a first argument.
Related
I have a SQL command wherein I am trying to join multiple columns in a single table which is,
dbo.tbl_Application AS APP on (APP.motor_no = SUBSTRING(R.remarks, CHARINDEX('|', R.remarks) + 1, LEN(R.remarks))) AND (Year(APP.date_created) = YEAR(R.txndate)) AND (APP.motor_no = M.motor_no)
Now I converted it to Linq
CONVERTED TO LINQ
join APP in db.tbl_Application on new
{ motor_no = R.remarks.Substring(5), R.txndate.Value.Year, motor_no2 = M.motor_no }
equals new
{ motor_no = APP.motor_no, APP.date_created.Value.Year, motor_no2 = APP.motor_no }
And then added to my existing linq which is:
EXISTING LINQ
from R in db.vwEtracs_Receipt
join RI in db.vwEtracs_ReceiptItem on new { parentid = R.objid } equals new { parentid = RI.parentid }
join IA in db.vwEtracs_IncomeAccount on new { objid = RI.acctid } equals new { objid = IA.objid }
join M in db.vwQVFS_Motor on new { motor_no = R.remarks.Substring(5) } equals new { motor_no = M.motor_no } into M_join
from M in M_join.DefaultIfEmpty()
join OOI in db.vwQVFS_OOI on new { operator_id = R.payerId } equals new { operator_id = OOI.operator_id } into OOI_join
from OOI in OOI_join.DefaultIfEmpty()
join CR in db.vwQVFS_COR on M.motor_id equals CR.motor_id into CR_join
from CR in CR_join.DefaultIfEmpty()
join F in db.vwQVFS_Franchise on new { or_id = R.objid } equals new { or_id = F.or_id } into F_join
from F in F_join.DefaultIfEmpty()
join T in db.vwQVFS_TODA on M.toda_id equals T.toda_id into T_join
from T in T_join.DefaultIfEmpty()
join RT in db.vwQVFS_RT on new { payer_id = R.payerId } equals new { payer_id = RT.payer_id } into RT_join
from RT in RT_join.DefaultIfEmpty()
join B in db.vwQVFS_Brand on M.brand_id equals B.brand_id into B_join
from B in B_join.DefaultIfEmpty()
If I include the line of code from above, I am getting the error Invalid length parameter passed to the LEFT or SUBSTRING function. but if I remove it, my Linq works perfectly.
What is making causing this error? Thanks in advance.
You getting the error because there is at least one record shorter than 5 characters in your database db.vwEtracs_Receipt.remarks column. So in your SQL, you don't pass length actually but in your linq you use Substring(int length). So the substring length can't be longer than the actual string length
How to convert the following query into linq
SELECT
a.ProductId,
a.Name,
a.Description,
b.Quoteid,
b.Productid,
b.Quantity,
b.OriginalPrice
FROM
Products AS a
LEFT JOIN
QuoteDtails AS b
ON a.ProductId = b.ProductId
AND b.QuoteId = 200;
Don't know where to add the AND condition.
Thanks and regards
You can try this linq if you want to write LEFT JOIN of linq, you need to add
into [temp collection] from [Left join talbe collection] in [temp collection].DefaultIfEmpty()
after Linq join
look like this.
from ss in Products
join aa in QuoteDtails
on ss.ProductId equals aa.ProductId into temp
from ds in temp.DefaultIfEmpty()
where ds.QuoteId = 200
select new
{
ProductId_P = ss.ProductId,
Name = ss.Name,
Description = ss.Description,
Quoteid = ds.Quoteid,
Productid_Q = ds.Productid,
Quantity = ds.Quantity,
OriginalPrice = ds.OriginalPrice
}
You can add AND condition in your LINQ query like this :
var res = from p in products
join q in quoteDtails on new { Criteria1 = p.ProductID, Criteria2 = 200 } equals new { Criteria1 = q.Productid, Criteria2 = q.Quoteid }
select new
{
ProductId_P = p.ProductID,
Name = p.Name,
Description = p.Description,
Quoteid = q.Quoteid,
Productid_Q = q.Productid,
Quantity = q.Quantity,
OriginalPrice = q.OriginalPrice
};
I have a products table and an activities table.Each product is made up of different activities.What i want is to display a product name,price,etc and the all the activities (i.e all the activities in one column) which are part of that product in a datagrid using linq
Below is my query
using (bungeedbEntities context = new bungeedbEntities())
{
var bookingData = from con in context.bookings
join agn in context.agents on con.main_agent_id equals agn.code
select new POS_LINK.BusinessObjects.Bookings
{
Product = con.product_name,
Activity = String.Join(",", (from con1 in context.bookings
join acp in context.booking_activity on con1.code equals acp.booking_code
join agn in context.agents on con1.main_agent_id equals agn.code
join act in context.activities on acp.activity_code equals act.code
select act.name).ToArray()),
ReservationCode = con.main_agent_voucher,
CostOfSale = 0.00M,
DateOfActivity = (DateTime)con.date_of_activity,
Notes = con.notes,
Quantity = (int)con.pax,
Child_quantity = 0,
Child_cost_percentage = 0,
CostPerPerson = 0.00M,
SubAgentRef = "56789",
SubAgentName = con.sub_agent_name,
ClientName = con.client_name,
MainAgent = agn.agent_name,
Consultant2 = con.sub_agent_consultant
};
return bookingData.ToList();
On running i get the following error- LINQ to Entities does not recognize the method 'System.String Join(System.String, System.String[])' method, and this method cannot be translated into a store expression.
I seem to have run out of ideas anyone with a better solution to this would save me from a lot of head scratching
You need to do it in two steps: first step selects the data, while the second step translates it to a string with string.Join:
var bookingData = (from con in context.bookings
join agn in context.agents on con.main_agent_id equals agn.code
select new { // Construct an anonymous type with the relevant parts
Product = con.product_name,
ActivityData = (from con1 in context.bookings
join acp in context.booking_activity on con1.code equals acp.booking_code
join agn in context.agents on con1.main_agent_id equals agn.code
join act in context.activities on acp.activity_code equals act.code
select act.name),
ReservationCode = con.main_agent_voucher,
DateOfActivity = (DateTime)con.date_of_activity,
Notes = con.notes,
Quantity = (int)con.pax,
SubAgentName = con.sub_agent_name,
ClientName = con.client_name,
MainAgent = agn.agent_name,
Consultant2 = con.sub_agent_consultant
}).AsEnumerable() // Bring this into memory
.Select(p => new POS_LINK.BusinessObjects.Bookings {
Product = p.Product,
Activity = string.Join(", ", p.ActivityData.ToArray()),
ReservationCode = p.ReservationCode,
CostOfSale = 0.00M,
Notes = p.Notes,
Quantity = p.Quantity,
Child_quantity = 0,
Child_cost_percentage = 0,
CostPerPerson = 0.00M,
SubAgentRef = "56789",
SubAgentName = p.SubAgentName,
ClientName = p.ClientName,
MainAgent = p.MainAgent,
Consultant2 = p.Consultant2
});
return bookingData.ToList();
The idea here is simple: first, you construct an anonymous type that has all relevant information, including an enumeration of acc.names, then you force it into memory by calling AsEnumerable(), and finally construct your POS_LINK.BusinessObjects.Bookings objects using LINQ-to-Object, which understands string.Join.
I have the following query in controller and I want to store a column value in a variable but I am not being able to iterate it. Here is my code:
var srmas = (
from SRMAs in db.SRMAs
join SRMAStatus in db.SRMAStatus on SRMAs.Status equals SRMAStatus.Id
join PurchaseOrders in db.PurchaseOrders on SRMAs.PONumber equals PurchaseOrders.PONumber
join Suppliers in db.Suppliers on PurchaseOrders.SupplierID equals Suppliers.SupplierID
join SRMADetails in db.SRMADetails on SRMAs.Id equals SRMADetails.SRMAId
where(SRMAs.Id == srmaid)
group SRMADetails by new
{
SRMADetails.Id,
SRMADetails.SRMAId,
SRMADetails.SupplierPartNum,
SRMAs.PONumber,
SRMAs.ActualAmount,
SRMAs.ApprovedOn,
SRMAs.Status,
SRMAs.TrackingNumber,
SRMAs.SupplierRMANumber,
SRMAs.RequestedFromSupp,
SRMAs.CreatedOn,
Suppliers.SupplierName,
SRMAStatus.StatusName,
PurchaseOrders.PODate,
PurchaseOrders.suppliersOrderNumber
} into grp
select new
{
grp.Key.Status,
grp.Key.SRMAId,
grp.Key.Id,
grp.Key.PONumber,
grp.Key.SupplierRMANumber,
grp.Key.ActualAmount,
grp.Key.SupplierPartNum,
grp.Key.RequestedFromSupp,
grp.Key.TrackingNumber,
grp.Key.ApprovedOn,
grp.Key.SupplierName,
grp.Key.StatusName,
grp.Key.PODate,
grp.Key.suppliersOrderNumber,
grp.Key.CreatedOn,
Sum = grp.Sum(SRMADetails => SRMADetails.Cost * SRMADetails.QtyReturned)
}
).ToList();
System.Collections.IEnumerable et = (System.Collections.IEnumerable)srmas;
IEnumerator it = et.GetEnumerator();
while (it.MoveNext())
{
SRMA current = (SRMA)it.Current;
Response.Write(current.Status);
}
ViewBag.SRMAs = srmas.Select(srma => new IndexViewModel
{
Id = srma.SRMAId,
SupplierRMANum = srma.SupplierRMANumber,
SRMADetailsID = srma.Id,
PONumber = srma.PONumber,
CreatedOn = srma.CreatedOn,
SupplierName = srma.SupplierName,
SRMAStatus = srma.StatusName,
Status = srma.Status,
suppliersOrderNumber = srma.suppliersOrderNumber,
PODate = srma.PODate,
Sum = srma.Sum,
TrackingNumber = srma.TrackingNumber,
ActualAmount = srma.ActualAmount
}).ToList();
I just want to get Status value of first record. How do I do it?
I am needing to make a left join and also use the select operator case.
My LINQ basic is this and works:
resultado.Dados =
(
from a in db.AgendaHorario
join b in db.Agenda on a.AgendaID equals b.AgendaID
join c in db.Profissional on a.ProfissionalID equals c.ProfissionalID into d
from e in d.DefaultIfEmpty()
select new
{
id = a.AgendaHorarioID,
Medico = e.Identificacao
});
But I must add a new field and it should be formatted, then my LINQ looked like this:
resultado.Dados =
(
from a in db.AgendaHorario
join b in db.Agenda on a.AgendaID equals b.AgendaID
join c in db.Profissional on a.ProfissionalID equals c.ProfissionalID into d
from e in d.DefaultIfEmpty()
select new
{
id = a.AgendaHorarioID,
Medico = e.Identificacao,
start = a.Horario.ToString("yyyy-MM-dd HH:mm:ss")
}
);
This error happens:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
If you add the ToList() or AsEnumerable() in db.AgendaHorario.ToList() and db.Agenda.ToList() and db.Profissional.ToList() error that appears is:
Object reference not set to an instance of an object.
What should I do to have a left join with case and fields and formatted fields
Try this:
resultado.Dados =
(
from a in db.AgendaHorario
join b in db.Agenda on a.AgendaID equals b.AgendaID
join c in db.Profissional on a.ProfissionalID equals c.ProfissionalID into d
from e in d.DefaultIfEmpty()
select new
{
id = a.AgendaHorarioID,
Medico = e.Identificacao,
start = a.Horario
}).AsEnumerable().Select(x => new
{
id = x.id,
Medico = x.Medico,
start = x.start.ToString("yyyy-MM-dd HH:mm:ss")
}
);
try to set the string in a variable then assign it to your query like this:
var myValue = Horario.ToString("yyyy-MM-dd HH:mm:ss");
resultado.Dados = (
from a in db.AgendaHorario
join b in db.Agenda on a.AgendaID equals b.AgendaID
join c in db.Profissional on a.ProfissionalID equals c.ProfissionalID into d
from e in d.DefaultIfEmpty()
select new
{
id = a.AgendaHorarioID,
Medico = e.Identificacao,
start = myValue
} );