Hi I am trying to do this query:
Select ProjInvoiceJour
where NOT (ProjInvoiceJour.ProjInvoiceType == ProjInvoiceType::Onaccount && ProjInvoiceJour.CountryRegionID != "ES")
But I need to do it whit querybuilder:
qbds.addRange(fieldnum(ProjInvoiceJour, ProjInvoiceType)).value(strfmt('!%1',
strfmt("((%1.%2 == %3) && (%4.%5 != '%6'))",
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, ProjInvoiceType),
any2int(ProjInvoiceType::OnAccount),
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, CountryRegionID),
queryvalue("ES"))));
But the query has some error:
SELECT * FROM ProjInvoiceJour WHERE ((NOT (ProjInvoiceType = 255)))
Thanks
The law of De Morgan comes to rescue:
select ProjInvoiceJour
where !(ProjInvoiceJour.ProjInvoiceType == ProjInvoiceType::Onaccount &&
ProjInvoiceJour.CountryRegionID != 'ES')
is equivalent to:
select ProjInvoiceJour
where ProjInvoiceJour.ProjInvoiceType != ProjInvoiceType::Onaccount ||
ProjInvoiceJour.CountryRegionID == 'ES'
Or in a query:
qbds.addRange(fieldnum(ProjInvoiceJour, ProjInvoiceType)).value(strfmt('((%1.%2 != %3) || (%4.%5 == "%6"))',
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, ProjInvoiceType),
0+ProjInvoiceType::OnAccount,
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, CountryRegionID),
'ES'));
Related
I have a table that I am filtering on. There is a four filters status, project, department, KPI
KPI is a boolean value. This filter has three value. True, False or both.
So, when the filter is true, it should return 'true' data, when the filter is 'false', return 'false' data; and when 'all' return both true and false data.
My code attached below, how can I enhance this code ? Can I write this with out if else conditions
var result = new List<TaskMaster>();
bool kpidata;
if (request.IsKPI == 1) { kpidata = true; } else { kpidata = false; }
//For KPI Tasks
if (request.IsKPI == 1 || request.IsKPI == 0)
{
result = await _context.TaskMaster.Where(c => c.IsActive && (c.Status == request.Status || request.Status == "All") && (c.ProjectId == request.ProjectId || request.ProjectId == 0) && (c.IsKPI == kpidata) && (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)).Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery().ToListAsync();
//For Non-KPI Tasks
}
else {
result = await _context.TaskMaster.Where(c => c.IsActive && (c.Status == request.Status || request.Status == "All") && (c.ProjectId == request.ProjectId || request.ProjectId == 0) && (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)).Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery().ToListAsync();
}
Revise the code, both queries are almost identical except c.IsKPI == kpidata part.
Few ways to remove the redundant:
Write the IQueryable query and append the .Where() if the condition meets. You are fine to append/extend the query before the query is materialized (via .ToList(), .First() etc.).
var result = new List<TaskMaster>();
bool kpidata;
if (request.IsKPI == 1) { kpidata = true; } else { kpidata = false; }
IQueryable<TaskMaster> query = _context.TaskMaster
.Where(c => c.IsActive
&& (c.Status == request.Status || request.Status == "All")
&& (c.ProjectId == request.ProjectId || request.ProjectId == 0)
&& (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0));
//For KPI Tasks
if (request.IsKPI == 1 || request.IsKPI == 0)
{
query = query.Where(c => c.IsKPI == kpidata);
}
result = await query.Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery()
.ToListAsync();
You can also migrate the if logic to the query too.
&& (!(request.IsKPI == 1 || request.IsKPI == 0) || c.IsKPI == kpidata)
If !(request.IsKPI == 1 || request.IsKPI == 0) returns true, the c.IsKPI == kpidata part will be omitted.
result = await _context.TaskMaster
.Where(c => c.IsActive
&& (c.Status == request.Status || request.Status == "All")
&& (c.ProjectId == request.ProjectId || request.ProjectId == 0)
&& (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)
&& (!(request.IsKPI == 1 || request.IsKPI == 0) || c.IsKPI == kpidata))
.Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery()
.ToListAsync();
This
(request.IsKPI == 1 || request.IsKPI == 0)
can also be revised to:
(new List<int> { 0, 1 }).Contains(request.IsKPI)
Hi i got question for the entity query. Please see my code
var list = from table in db.USER_BETTINGS
where table.UserID == UserId
&& table.UserID !="admin"
//&& table.WinningAfterRebate != 0m
&& table.BettingTransactionTime >= fromDate &&
table.BettingTransactionTime <= toDAte
//&& table.WinningAfterRebate !=0m
// orderby table.BettingTransactionNumber descending//table.BettingNumber descending//, table.BettingTransactionTime descending//
select table;
if (loteryNumber != 0)
{
list= list.Where(x => x.LotteryNumber == loteryNumber);
}
if (gameNum != 0)
{
list= list.Where(x => x.GameNumber == gameNum);
}
if (periodDate != "")
{
list= list.Where(x => x.PeriodDate == periodDate);
}
if (close.Equals("Y"))
{
list= list.Where(w => w.WinningAfterRebate != 0m);
}
else
{
list= list.Where(x => x.WinningAfterRebate == 0);
}
But the list not filtering , It return all record? Anyone got face this problem before?
You need some makeover in this code. You have unnecessarily used so many if condition, we need to get rid of that first. Change your query with the following.
var list =( from table in db.USER_BETTINGS
where table.UserID == UserId
&& table.UserID !="admin"
&& table.BettingTransactionTime >= fromDate
&& table.BettingTransactionTime <= toDAte
&& table.LotteryNumber == (loteryNumber != 0 ? loteryNumber : table.LotteryNumber)
&& table.GameNumber == (gameNum != 0 ? gameNum : table.GameNumber)
&& table.PeriodDate == (periodDate != string.Empty ? periodDate : table.PeriodDate )
&& (close.Equals("Y") ? table.WinningAfterRebate != 0 : table.WinningAfterRebate == 0)
).ToList();
DataContext db = new DataContext();
Select_Utilities SelectedUtility = (from su in db.Select_Utilities
where su.id == SelectUtilityId
&& su.Worksite_Id == WorksiteId
&& su.Utility_Company.id == UtilityCompanyId
select su).FirstOrDefault();
then I wanted to say SelectedUtility.comment = "whatever the comment may be";
BUT getting ERROR: cannot implicity convert type 'Select_Utility' to 'Select_Utilities'
with 'FirstOrDefault' in statement....any advice?
Thanks
Change result type to Select_Utility
Select_Utility SelectedUtility = // here
(from su in db.Select_Utilities
where su.id == SelectUtilityId &&
su.Worksite_Id == WorksiteId &&
su.Utility_Company.id == UtilityCompanyId
select su).FirstOrDefault();
I'm guessing Select_Utilities is the name of your Entity table which ruturns a collection of Select_Utility objects. Try:
Select_Utility selectedUtility = (from su in db.Select_Utilities
where su.id == SelectUtilityId
&& su.Worksite_Id == WorksiteId
&& su.Utility_Company.id == UtilityCompanyId
select su).FirstOrDefault();
My Owner class has 6 string attributes. I want to write a JDO query which should check a string with those all 6 attributes of Owner class and if any of those 6 attribute matches then that owner object should be added in list. I am using following JDO query syntax but its not working:
String branchName = req.getParameter("branch");
List<Owner> owners = null;
Query query = pm.newQuery(Owner.class);
query.setFilter("branch1 == branchParam || branch2 == branchParam || branch3 == branchParam || branch4 == branchParam || branch5 == branchParam || branch6 == branchParam");
query.declareParameters("String branchParam");
companies = (List<Owner>) query.execute(branchName);
i am getting following exceptoin:
org.datanucleus.store.appengine.query.DatastoreQuery$UnsupportedDatastoreFeatureException: Problem with query <SELECT FROM com.eplinovo.stallbokenapp.domain.HastForetag WHERE branch1 == branchParam || branch2 == branchParam || branch3 == branchParam || branch4 == branchParam || branch5 == branchParam || branch6 == branchParam PARAMETERS String branchParam>: Or filters cannot be applied to multiple properties (found both branch1 and branch2).
Any idea how can i write a query for that. Thanks in advance.
return (from s in db.StudentMarks
where s.Class == Class && s.Year == Year // this line
orderby s.Year, s.ExamType
select new StudentAddMarks()
{
--Obj
}).ToList();
I am going to return an Object depending on the Class and Year params. I want to remove the where condition when the Class and Year parames are null or zero.
Any ideas please.
Just add a clause handling null or zero values as well:
where ((Class != null && Class != 0) ? s.Class == Class : true) &&
((Year != null && Year != 0) ? s.Year == Year : true)
The above code uses the shorthand if-then-else syntax, that works as follows:
value = (condition ? if_true : if_false);
// ...is equivalent to...
if (condition)
{
value = if_true;
}
else
{
value = if_false;
}
This should work:
where (s.Class == Class && s.Year == Year) || Class == null ||
Class == 0 || Year == null || Year == 0