I have this query that i've been trying to figure out how to convert to LINQ:
select bjbecd, bjbesk, areotx
from insku
inner join iniwre on bjcomp=a7comp and bjbecd=a7becd and bjbesk=a7besk
inner join initem on bjcomp=arcomp and bjbecd=arbecd
where a7comp=1 and
a7wcde in (1,10) and
a7ohdq>0 and rtrim(a7becd) + rtrim(a7besk) not in
(select skucode from eoditems)
And here is my LINQ so far:
(from i in db.INSKUs
join w in db.INIWREs on
new { i.BJCOMP, i.BJBECD, i.BJBESK }
equals
new { w.A7COMP, w.A7BECD, w.A7BESK }
join t in db.INITEMs on
new { i.BJCOMP, i.BJBECD }
equals
new { t.ARCOMP, t.ARBECD }
where w.A7COMP == 1
where w.A7WCDE == 1 || w.A7WCDE == 10
where w.A7OHDQ > 0
where !(from z in db.EODItems
select z.SkuCode).Contains(w.A7BECD.TrimEnd() + w.A7BESK.TrimEnd())
select new { i.BJBECD, i.BJBESK, t.AREOTX }
);
I am getting an error message on the first join stating"The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'."
All searches I did, related to type match errors, but I quadruple checked all my types within the joins, and they are the same.
Try to do something like this :
join w in db.INIWREs on
new { i.BJCOMP, i.BJBECD, i.BJBESK }
equals
new { BJCOMP = w.A7COMP, BJBECD = w.A7BECD, BJBESK = w.A7BESK }
Should work.
Related
I'm trying to return a list from a join query and having difficulty with the errors..
here is my code:
public IList<UniStock.Domain.Tables.Inventory> SelectInventoryListByColourSizeGroup(string styleColour, string sizeGroup)
{
var db = new UniStockContext();
IQueryable<Domain.Tables.Inventory> q = (from c in db.Inventories
join o in db.SizeGroupSizes
on c.Size.Trim() equals o.Description.Trim()
where (c.SytleColour == styleColour)
&& (o.SizeGroup.Description == sizeGroup)
select new
{
c
});
return q;
}
Error I'm seeing now is:
Cannot implicitly convert type 'System.Linq.IQueryable' to System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)`
The problem is, as the error says, you cannot return an anonymous type when your method is expecting a specific type. By using the new {} syntax you are creating an anonymous type. Simply remove new {}
public IList<UniStock.Domain.Tables.Inventory> SelectInventoryListByColourSizeGroup(string styleColour, string sizeGroup)
{
var db = new UniStockContext();
IQueryable<Domain.Tables.Inventory> q = (from c in db.Inventories
join o in db.SizeGroupSizes
on c.Size.Trim() equals o.Description.Trim()
where (c.SytleColour == styleColour)
&& (o.SizeGroup.Description == sizeGroup)
select c);
return q.ToList();
}
This part of your LINQ expression
select new { c } ;
Will give you a list of annonymous type objects. Your method signature says it should return an IList<Inventory>. Since you are not returning the expected type, you are getting this compile time error.
You select should be just c which is an alias for db.Inventories ( with the filter though)
var q = from c in db.Inventories
join o in db.SizeGroupSizes
on c.Size.Trim() equals o.Description.Trim()
where (c.SytleColour == styleColour)
&& (o.SizeGroup.Description == sizeGroup)
select c;
return q.ToList();
The variable q will be IQueryable<Inventory> and when you call the ToList() it will execute the LINQ expression and will get a List<Inventory> and you are returning that, which is matching with your method signature (IList<Signature>)
You are projecting into a new AnonymousType by doing this:
select new { c }
What you probably want is to select the object itself like this:
select c
Then you will want to append a .ToList() to the end to execute the query and populate the list.
I'm having problem translating a query to LINQ in C# this is my query
select PDF.Name,PDF.Name
from PDF inner join PC
on PDF.Id=PC.Ref_PDF
having pc.Ref_Customer=_id
you should know that _id is something that i send to my method so I can find something with it
so far I did this which I don't think would work(cuase lot's of errors poped up)
Invalid expression term 'select'
and
Expected contextual keyword 'equals'
both at end of here join p in Context.PDFs on c.Ref_PDF
internal List<EF_Model.PDF> Customers_File(int _id)
{
using (var Context = new EF_Model.CoolerEntities())
{
var q = from c in Context.PCs
where c.Ref_Customer == _id
join p in Context.PDFs on c.Ref_PDF
select new { c.PDF.Id, c.PDF.Name, c.PDF.File };
return q;
}
}
How can we make it into a linq statement?
Fix the syntax for the query
List<EF_Model.PDF> Customers_File(int _id) {
using (var Context = new EF_Model.CoolerEntities()) {
var q = from c in Context.PCs
join p in Context.PDFs on c.Ref_PDF equals p.Id
where c.Ref_Customer == _id
select new EF_Model.PDF { Id = c.PDF.Id, Name = c.PDF.Name, File = c.PDF.File };
return q.ToList();
}
}
and the method expects to return a list so use the ToList() on the query when returning from the method.
UPDATE:
If the intention was just to return the PDF model then no need to create the anonymous object just return c.PDF
List<EF_Model.PDF> Customers_File(int _id) {
using (var Context = new EF_Model.CoolerEntities()) {
var q = from c in Context.PCs
join p in Context.PDFs on c.Ref_PDF equals p.Id
where c.Ref_Customer == _id
select c.PDF;
return q.ToList();
}
}
This should do the job for you
from pc in context.PCs where pc.Ref_Customer == _id
join p in context.PDFs on pc.Ref_PDF equals p.Ref_PDF
select new {pc.PDF.Id, pc.PDF.Name, pc.PDF.File }
Probably when you said errors, I assume you saw synactical errors
If you set up a navigation property, the query is:
var q =
from pc in Context.PCs
where pc.Ref_Customer == _id
from pdf in pc.PDFs
select pdf;
If you don't:
var q =
from pc in Context.PCs
where pc.Ref_Customer == _id
join pdf in Context.PDFs on pc.Ref_PDF equals pdf.Id
select pdf;
The main thing to know about the join syntax, it has the form
" join (a) in (b) on (c) equals (d) "
(a): the new range variable for a member of (b)
(b): the source of items you are joining to - the right side of the join.
(c): an expression in which the item from the left side of the join is in scope.
(d): an expression in which the item from the right side of the join is in scope - (a).
public IQueryable<SMMSALESUNIT> GetPersonalSalesUnitQuery()
{
var q = from r in dax.SMMSALESUNITs
where r.DATAAREAID == COMPANYID
&& r.ACTIVE == 1
select r;
}
public IQueryable<SMMSALESUNITMEMBER> GetPersonalSalesUnitMemberQuery()
{
IQueryable<SMMSALESUNIT> salesUnit = new SMMSALESUNIT().GetPersonalSalesUnitQuery();
var q = from r in dax.SMMSALESUNITMEMBERs
join s in salesUnit on r.SALESUNITID equals s.SALESUNITID
select r;
return q;
}
On q.ToList() the following error occurs:
"The query contains references to items defined on a different data context"
Problem is (most likely) here:
IQueryable<SMMSALESUNIT> salesUnit = new SMMSALESUNIT().GetPersonalSalesUnitQuery();
dax used in GetPersonalSalesUnitQuery seems to be instance member of class SMMSALESUNIT. Here you create new SMALLSALESUNIT and so new instance of dax. Then you do
var q = from r in dax.SMMSALESUNITMEMBERs
join s in salesUnit on r.SALESUNITID equals s.SALESUNITID
select r;
This is another dax, not the instance member of SMALLSALESUNIT you created above. So you try to join queries from two different contexts, which fails.
I have this query wit a group join:
foreach (var item in someList)
{
var result = (from t1 in someContext.Table1
join t2 in someContext.Table2 on new { t1.SomeID, item.SomeName} equals new {t2.SomeID, t2.SomeName} into j1
...
}
I would like to know if it is possible to have a group join as above?
new { t1.SomeID, item.SomeName} equals new {t2.SomeID, t2.SomeName}
item.SomeName comes from the list i am iterating through.
If not, how would i change the statement to get the desired results?
The types of the properties used with the equals expression must match. So for example is Table1.SomeID is Int32 and Table2.SomeID is Nullable<Int32>, then they don't match.
EDIT
foreach (var item in someList)
{
var someName = item.SomeName;
var result = (from t1 in someContext.Table1
join t2 in someContext.Table2 on
new { t1.SomeID, SomeName = someName}
equals new { t2.SomeID, t2.SomeName} into j1
...
}
Also check item.SomeName is same type as t2.SomeName
In this case you must be sure that the properties and type of the two new anonymous objects are the same. I usually give specific name of properties.
Ex.:
var result = from t1 in someContext.Table1
join t2 in someContext.Table2 on
new { SomeID = t1.SomeID, SomeName = someName} equals
new { SomeID = t2.SomeID, SomeName = t2.SomeName} into j1
In the following LINQ statement I get the error :
Cannot implicitly convert type 'System.Linq.IQueryable' to 'bool'
The 3rd last line has a blue line under the entire line. Here is the code, I tried various changes, i must be missing something obvious, thanks in advance...
var allWaste = _securityRepository.FindAllWaste(userId, SystemType.W);
var searchResults = (
from s in allWaste
where
(
from x in _db.WasteIndicatorItems
join y in
_db.WasteIndicators
on x.WasteIndicatorId equals y.WasteIndicatorId
join z in
_db.HazardTypes
on y.HazardTypeId equals z.HazardTypeId
where
s.WasteId == x.WasteId
group new { x, z } by new { x.WasteId, z.Hazardous } into g
select new
{
nbrOf = g.Count(),
g.Key.Hazardous
}
).Where(a => a.nbrOf >= 1 && a.Hazardous == false)
select s
).Distinct();
The problem is you have an IQueryable in your where clause.
Try
...where( (from x in _db.WasteIndicators ... ).Any() )...
Or something that returns a boolean.