I'm trying to write a single query which will include one of the two conditions, based on an input variable:
!(from o in db.Products.Where(x => x.Company_ID == cid && x.IsDeleted != true)
or
(from o in db.Products.Where(x => x.Company_ID == cid && x.IsDeleted != true)
My current method, covering the former condition, is as follows. I have included productExists, which will be the parameter which determines whether I want condition #1 or #2 from above.
public IQueryable<ProductImportViewModel> AllImports(int id, bool productExists)
{
return (from t1 in db.Products_Staging
where (t1.ImportFileId == id) && !(from o in db.Products.Where(x => x.Company_ID == cid && x.IsDeleted != true)
select o.ProductName).Contains(t1.ProductName)
select new ProductImportViewModel
{
Id = t1.Id
}
}
If anybody could help me with this, I'd be much appreciated.
Something like this maybe:
where (t1.ImportFileId == id) &&
(
productExists==true
&&
!(from o in db.Products.Where(x => x.Company_ID == cid && x.IsDeleted != true).
Select(o=> o.ProductName).Contains(t1.ProductName)
)
||
(
productExists==false
&&
(from o in db.Products.Where(x => x.Company_ID == cid && x.IsDeleted != true).
Select(o=> o.ProductName).Contains(t1.ProductName)
)
You could also do it something like this:
var query=(from o in db.Products
.Where(x => x.Company_ID == cid && x.IsDeleted != true).
Select(o=> o.ProductName);
------
where (t1.ImportFileId == id) &&
(
productExists && !query.Contains(t1.ProductName)
)
||
(
!productExists && query.Contains(t1.ProductName)
)
Both queries will result in the same sql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With