Can anyone help me to make below query as linq
SELECT E.Cosem_Object, COALESCE(A.Explanation, 'Not Available' ) As
Explanation ,A.Unit, E.Reading , E.Unit, E.Meter_Date, E.Meter_Time from
ENERGY_PROFILE E LEFT OUTER JOIN ALL_COSEM_OBJECTS A on
(SUBSTRING(E.Cosem_Object,0,CHARINDEX('*',E.Cosem_Object + '*',0))) = A.Short_Cosem_Object
Updated:
I try
var query = from o in ENERGY_PROFILE
join e in ALL_COSEM_OBJECTS
on o.Short_Cosem_Object equals e.Short_Cosem_Object
select new {
o.Short_Cosem_Object, o.READING, e.EXPLANATION, e.UNIT
};
I need to add below line
SUBSTRING(E.Cosem_Object,0,CHARINDEX('*',E.Cosem_Object + '*',0)) in linq query
here's the exact conversion of your SQL query into LINQ, though I'm not sure if it's exactly what you are expecting since the lack of a sample and expected result:
var set = from E in ENERGY_PROFILE
join A in ALL_COSEM_OBJECTS
on E.Cosem_Object.Substring(0, (E.Cosem_Object + "*").IndexOf('*')) equals A.Short_Cosem_Object
into joinedTb
from c in joinedTb.DefaultIfEmpty()
select new
{
E.Cosem_Object,
Explanation = c.Explanation ?? "Not Available",
c.Unit,
E.Reading,
Unit2 = E.Unit,
E.Meter_Date,
E.Meter_Time
};
Related
I'm trying to make a query in LINQ translating the following T-SQL query:
select b.crs,
d.epg,
d.m,
Count (a.sq_rea) as qt
from t_aaaa a with (nolock)
join t_bbbb b with (nolock) on a.crs = b.crs
join t_cccc c with (nolock) on a.spr = c.spr and c.rsp = 1
join v_dddd d with (nolock) on c.epg = d.epg
Where a.st = 5
group by b.crs, d.epg, d.m
However, I cannot use the aggregate function like this:
var llstData = (from a in lobjaaaaDao.list()
join b in lobjbbbbDao.list() on a.p equals b.p
join c in lobjcccc.listar() on a.u equals c.u
from d in lobjddddDao.list()
where a.w == d.w &&
a.st.Equals(5) &&
d.Indent == false
group a by new { b.xxxx, c.yyyy, c.zzzz } into grp
select new
{
grp.Key.xxxx,
grp.Key.yyyy,
grp.Key.zzzz,
total = a.kkkk.Count() // ERROR ON THIS LINE
}
).ToList();
I am getting this error:
The name a does not exist in th current context
How can I fix the error?
In the last select statement the only parameter from the query you are accessible to is grp; where each group is IGrouping; each group is represented by a key and an IEnumerable which is in your case : IEnumerable<a>. Having said that; you can replace the line total = a.kkkk.Count() with total = grp.SelectMany(v=> v.kkkk).Count() OR total = grp.Sum(a => a.kkkk.Count()) to resolve the issue.
This way total represents the summation of kkkk counts in each a in the group.
I have a syntax problem and need some smart folks than me.
Here is my select statement
var Materials = from j in db.Jobs
join je in db.a_Job_Extensions on j.Top_Lvl_Job equals je.Job
join mr in db.Material_Reqs on j.Job1 equals mr.Job
join m in db.Materials on mr.Material equals m.Material1
where jobList.Contains(j.Top_Lvl_Job)
select new
{
je.PCR,
mr.Job,
j.Top_Lvl_Job,
OrderQty = ((from j1 in db.Jobs where j1.Job1 == j.Top_Lvl_Job select new { j1.Order_Quantity}).FirstOrDefault()).ToString(),
j.Part_Number,
mr.Material,
mr.Description,
mr.Est_Qty,
m.Status,
theClass = m.Class == null? "": m.Class
};
The j1.Order_Quantity is a double in the Job table. But when I run the query, it returns a value of "{ Order_Quantity = 607 }".
My desire is to have it return just the value of "607".
What am I doing wrong here?
The code was originally trying to bring it out as a double, but I switch to string to see what was coming out. I'm coming from a sql background so I must be missing something silly.
Try this
((from j1 in db.Jobs where j1.Job1 == j.Top_Lvl_Job select
j1.Order_Quantity.ToString()).FirstOrDefault())
I am getting the error
LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
Before my question gets called as a duplicate please read it completely. I have searched around and looked at a few solutions for this but none that seems to suit this..
var query = from royalHIstory in ctx.tblRoyalHistories
join historyComment in ctx.tblRoyalHistoryComments
on royalHIstory.RoyalHistoryID equals historyComment.RoyalHistoryID
orderby royalHIstory.IndexNum ascending
where royalHIstory.InstNmbr == instnmbr
select new
{
RoyalHistoryID = royalHIstory.RoyalHistoryID,
RoyalHistoryCommentID = historyComment.RoyalHistoryCommentID,
InstNmbr = royalHIstory.InstNmbr,
IndexNum = royalHIstory.IndexNum,
RoyalIns = royalHIstory.RoyalIns,
RoyalComment = historyComment.Comment,
Name = (from memberName in ctx.tblMembers
join instruct in ctx.tblInstructors
on memberName.MemberID equals instruct.MemberID
where Convert.ToInt32(instruct.InstructorInstrNo) == royalHIstory.RoyalIns
select memberName.MemberFirstName + " " + memberName.MemberLastName).FirstOrDefault()
};
The error lands on my where clause, in the conversion
where Convert.ToInt32(instruct.InstructorInstrNo) == royalHIstory.RoyalIns
I need that conversion to happen
Convert.ToInt32(instruct.InstructorInstrNo)
and I don't know how to force it to happen
you can't use
Convert.ToInt32(instruct.InstructorInstrNo)
in LINQ query, if you want to convert to int try this
var query = (from royalHIstory in ctx.tblRoyalHistories
join historyComment in ctx.tblRoyalHistoryComments
on royalHIstory.RoyalHistoryID equals historyComment.RoyalHistoryID
orderby royalHIstory.IndexNum ascending
where royalHIstory.InstNmbr == instnmbr
select new {royalHIstory,historyComment }).AsEnumerable().Select(row=>new
{
RoyalHistoryID = row.royalHIstory.RoyalHistoryID,
RoyalHistoryCommentID = row.historyComment.RoyalHistoryCommentID,
InstNmbr = row.royalHIstory.InstNmbr,
IndexNum = row.royalHIstory.IndexNum,
RoyalIns = row.royalHIstory.RoyalIns,
RoyalComment = row.historyComment.Comment,
Name = (from memberName in ctx.tblMembers
join instruct in ctx.tblInstructors
on memberName.MemberID equals instruct.MemberID
where Convert.ToInt32(instruct.InstructorInstrNo) == row.royalHIstory.RoyalIns
select memberName.MemberFirstName + " " + memberName.MemberLastName).FirstOrDefault()
});
I have Linq query where I am trying to write subquery to set the value of item, I am encountering with error saying
{ORA-00907: missing right parenthesis
}, Please let me know the right syntax to make it work
var lst = (
from cr in dbContext.Company
orderby cr.COMPANY_KEY
select new CompanyDto()
{
CompanyKey = cr.CompanyKey,
CompanyCode = (from rc in dbContext.COMPANY_PORTFOLIOS where rc.PORTFOLIO == cr.P_PORTFOLIO select rc.COMPANY_CODE).FirstOrDefault()
}
);
var d = st.Skip(pageIndex).Take(pageSize).ToList();
Even the below piece of code is not working
var lst = (
from cr in dbContext.Company
orderby cr.COMPANY_KEY
select new CompanyDto()
{
CompanyKey = cr.CompanyKey,
CompanyCode = (from rc in dbContext.COMPANY_PORTFOLIOS where rc.PORTFOLIO == cr.P_PORTFOLIO select rc.COMPANY_CODE).Single()
}
);
var d = st.Skip(pageIndex).Take(pageSize).ToList();
I think you can just use a Join to achieve what you are looking to do:
var lst = (
from cr in dbContext.Company
orderby cr.COMPANY_KEY
join rc in dbContext.COMPANY_PORTFOLIOS on cr.PORTFOLIO equals rc.P_PORTFOLIO into myJoin
from rc in myJoin.DefaultIfEmpty()
select new CompanyDto()
{
CompanyKey = cr.CompanyKey,
CompanyCode = rc.COMPANY_CODE
}
);
var d = lst.Skip(pageIndex).Take(pageSize).ToList();
If the above still gives Oracle errors, put a breakpoint at the last line and get the SQL the linq generates. You can then paste this into something like Oracle SQL Developer and run the query, which may give you a more informative error message and allow you to track down the issue.
I want to convert this query into linq, please help:
select
mr_ssample.objectid,
mr_ssample.stcode
from mr_ssample
inner join mr_wsample on mr_ssample.objectid = mr_wsample.objectid
And mr_ssample.stcode in( select stcode
from mr_wsample)
i tried this in C#
var query = from ssamp in marineEntity.MR_SSAMPLE
join wsamp in marineEntity.MR_WSAMPLE on ssamp.OBJECTID equals wsamp.OBJECTID && ssamp.stcode.contains(wsamp.stcode)
select new
{};
However, I could not access wsamp in contains, or I dont know the alternative of this.
give this a try,
var _result = from a in mr_ssample
join b in mr_wsample on a.objectid equals b.objectid
where (from c in mr_wsample select new {c.stcode})
.Contains(new {a.stcode})
select new {a.objectid, a.stcode}