I had the code below that provide search, 4 field, the
lambda will increase huge lot for each field included in, and it is time consuming and not efficient to have such a combination search, any better way to write a proper search?
var result = db.ValuePairs
.Where(c=>
(isStatus && isText && isCode && isGroupCd)
?
c.Status == status && c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText && isCode)
?
c.Status == status && c.Text == text && c.Code == code
:
(isStatus && isText && isGroupCd)
?
c.Status == status && c.Text == text && c.GroupCode == groupCd
:
(isStatus && isCode && isGroupCd)
?
c.Status == status && c.Code == code && c.GroupCode == groupCd
:
(isText && isCode && isGroupCd)
?
c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText)
?
c.Status == status && c.Text == text
:
(isStatus && isCode)
?
c.Status == status && c.Code == code
:
(isStatus && isGroupCd)
?
c.Status == status && c.GroupCode == groupCd
:
(isText && isCode)
?
c.Text == text && c.Code == code
:
(isText && isGroupCd)
?
c.Text == text && c.GroupCode == groupCd
:
(isCode && isGroupCd)
?
c.Code == code && c.GroupCode == groupCd
:
(isStatus)
?
c.Status == status
:
(isText)
?
c.Text == text
:
(isCode)
?
c.Code == code
:
(isGroupCd)
?
c.GroupCode == groupCd
:
groupCd != ""
);
Pretty simple boolean logic there...
.Where(c =>
(!isStatus || c.Status == status)
&& (!isCode || c.Code == code)
&& (!isGroupCd || c.GroupCode == groupCd)
);
Related
{
"__name__":"/databases/(default)/documents/users/mfBBtfSX91YS9mXrkmEDTksA0712/sold/mfBBtfSX91YS9mXrkmEDTksA0712-hanSW0s5MxOOENWnc5T1cLinekj2/invoice/WhdEi2W83GtjDjowi6MV",
"id":"WhdEi2W83GtjDjowi6MV",
"data":{
"id":"db1a01a0-4da5-11ec-a579-c31f34aa6119",
"total":80,
"quotations":[
{
"id":"c595e610-46ab-11ec-b8ad-d56159e1778c",
"title":"anda",
"measuremntUnit":"kg",
"rate":35,
"quantity":2,
"isSelected":false,
"index":1
},
{
"id":"c595e610-46ab-11ec-b8ad-d56159e1778c",
"title":"biscuit",
"measuremntUnit":"packet",
"rate":10,
"quantity":1,
"isSelected":true,
"index":7}
],
"sellerEmail":"ku1438693#gmail.com",
"sellerUserId":"mfBBtfSX91YS9mXrkmEDTksA0712",
"sellerDisplayName":" Lila Kunwar",
"buyerUserId":"hanSW0s5MxOOENWnc5T1cLinekj2",
"buyerDisplayName":"saugat thapa",
"buyerEmail":"nexus.saugat26#gmail.com",
"sellerPhotoUrl":"https://lh3.googleusercontent.com/a/AATXAJ",
"buyerPhotUrl":"https://lh3.googleusercontent.com/a/AATXAJ"
}}
i am trying to check whether above data can insert into firestore given that the following is the security rule. however i get error Error running simulation — An unknown error occurred.
match /users/{userId}{
match /sold/{soldId}/{document=**}{
function isAuthed(){
return request.auth != null && request.auth.uid == userId;
}
function isSeller(){
return request.auth.uid == resource.data.sellerUserId ||
request.auth.uid == resource.data.buyerUserId;
}
function isApproved() {
return request.resource.data.isApproved == false;
// || request.resource.data.reviewed == "false"
}
function quotations(){
return request.resource.data.quotations;
}
function isOtherThanQuotations(){
return
isValidStringInput(request.resource.data.buyerUserId) &&
isValidStringInput(request.resource.data.buyerEmail) &&
isValidStringInput(request.resource.data.buyerPhotoUrl) &&
isValidStringInput(request.resource.data.buyerDisplayName) &&
isValidStringInput(request.resource.data.sellerUserId)&&
isValidStringInput(request.resource.data.buyerEmail) &&
isValidStringInput(request.resource.data.buyerPhotoUrl) &&
isValidStringInput(request.resource.data.buyerDisplayName) &&
isValidNumber(request.resource.data.total) &&
// isApproved() &&
request.resource.data.createdAt == request.time;
}
function isSelected() {
return request.resource.data.isSelected == false;
// || request.resource.data.reviewed == "false"
}
function isValidQuotation(item) {
return quotations()[item].keys().hasAll(
[
'title',
'rate',
'quantity',
'measuremntUnit',
'isSelected',
'index',
])
&& isValidStringInput(quotations()[item].title, 200)
&& isValidNumber(quotations()[item].rate)
&& isValidNumber(quotations()[item].quantity)
&& isValidStringInput(quotations()[item].measuremntUnit, 200)
&& isValidNumber(quotations()[item].index)
&& isSelected();
}
function isValidStringInput(input, maxSize) {
return input is string
&& input.size() > 0
&& input.size() <= maxSize;
}
function isValidNumber(input) {
return input is int
|| input.matches('^[0-9]+$');
}
allow read: if isSeller();
allow write: if isAuthed()
&& quotations().size() >= 1
&& quotations().size() <= 10
&& isOtherThanQuotations()
&& isValidQuotation(0)
&& (quotations().size() < 2 || isValidquotation(1) )
&& (quotations().size() < 3 || isValidquotation(2) )
&& (quotations().size() < 4 || isValidquotation(3) )
&& (quotations().size() < 5 || isValidquotation(4) )
&& (quotations().size() < 6 || isValidquotation(5) )
&& (quotations().size() < 7 || isValidquotation(6) )
&& (quotations().size() < 8 || isValidquotation(7) )
&& (quotations().size() < 9 || isValidquotation(8) )
&& (quotations().size() < 10 || isValidquotation(9) )
&& (quotations().size() < 11 || isValidquotation(10) );
}
}
is it becase i have too too many functions? right now myconcern is with create only.
is it becasue the id doesnt have security rule to validate? i tried providing validation for id as well but the error still persist. i need more than 10 map in an array but this is for simplicity of testing process.
In c# or others we can use this in IF
ex.
if((a=1 && b=2)
(c=2 && d=3)
){}
using ( inside ( to check bool in that statement.
But in swift
I'm trying to make something like this.
func IsWorkDay() -> Bool {
if (monWorkYN == "Y" , weekday == 2)
|| (tueWorkYN == "Y" , weekday == 3)
|| (wedWorkYN == "Y" , weekday == 4)
{
return true
}
}
But the ( ) doesn't work here. Is there a way to do this?
In this case you have to use the && operator, with a comma the compiler treats the expression in parentheses as a tuple.
And please name functions (and variables) with starting lowercase letter
func isWorkDay() -> Bool {
if (monWorkYN == "Y" && weekday == 2)
|| (tueWorkYN == "Y" && weekday == 3)
|| (wedWorkYN == "Y" && weekday == 4)
{
return true
}
return false
}
or simpler
func isWorkDay() -> Bool {
return (monWorkYN == "Y" && weekday == 2)
|| (tueWorkYN == "Y" && weekday == 3)
|| (wedWorkYN == "Y" && weekday == 4)
}
You can use this property instead of your function
var isWorkDay: Bool {
return (monWorkYN == "Y" && weekday == 2)
|| (tueWorkYN == "Y" && weekday == 3)
|| (wedWorkYN == "Y" && weekday == 4)
}
Resource
You can check this for learn more
Swift Documentation
2017-10-22 18:18:02.498 studycook[17238]
[Firebase/Database][I-RDB038012] setValue: or removeValue: at
/users/user_id/test/abc
failed: permission_denied
The security settings for /users/$uid are as follows:
".read": "auth != null && auth.uid == $uid || auth != null && root.child('admins').child(auth.uid).val() == true",
".write": "auth != null && auth.uid == $uid || auth != null && root.child('admins').child(auth.uid).val() == true"
What am I doing wrong?
My code:
ref.child("users/\(Auth.auth().currentUser!.uid)/test/abc").setValue("true")
i got stuck on this query that calculate the new column.
i cannot explain briefly just see the snippet code below.
from user in context.Table
select new
{
Total = user.Total,
Paid = user.Paid,
Balance = //should be Total - Paid to assign result
}
i have tried this query
var result = from a in context.EnrollmentRequests
where a.SchoolYear == SchoolYear
select new
{
a.StudentID,
Name = a.Student.FirstName + " " + a.Student.MiddleName + " " + a.Student.LastName,
Tuition = context.Miscs.Where(m => m.YearLevel == a.YearLevel && m.SchoolYear == SchoolYear && m.Term == a.Term && m.CourseID == a.CourseID)
.Select(ms => new { Amount = ms.Amount })
.Union(context.StudentCharges
.Where(s => s.YearLevel == a.YearLevel && s.SchoolYear == SchoolYear && s.Term == a.Term && s.CourseID == a.CourseID && s.StudentID == a.StudentID)
.Select(ss => new { Amount = ss.Amount }))
.Union(context.StudentSubjectTakes
.Where(st => st.StudentID == a.StudentID && st.SchoolYear == a.SchoolYear && st.Term == a.Term && st.YearLevel == a.YearLevel && st.EducationalLevel == a.Student.StudentAdvanceEducations.FirstOrDefault().EducationLevel)
.Select(st => new
{
Amount = context.SubjectOfferedFees
.Where(f => f.SubjectsOfferedID == st.SubjectsOfferedID).Sum(w => (decimal?)w.Cost ?? 0)
}))
.Select(f => f.Amount).Sum(),
PaymentMade = context.Payments.Where(p => p.SchoolYear == SchoolYear && p.Term == a.Term && p.StudentID == a.StudentID && p.PaymentDes == "Tuition Fee").Sum(sm => (decimal?)sm.Amount),
Balance = Tuition - PaymentMade //Does not exist on current context
};
but doesn't work it says that does not exist on current context.
how could this possible.
thanks. this will be helpful to anyone.
Balance = user.Total - user.Paid
I just updated to the non beta version of Xcode 6 (finally) and, coming from beta 5, got a few errors that I didn't get before, one being "could not find overload for '&&' that accepts the supplied arguments"
I am following a tutorial here and from another question, I know that this error is because "the expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions."
I'm a beginner; how do I break up an expression into sub-expressions?
My code:
func checkForWin(){
//first row across
var youWin = 1
var theyWin = 0
var whoWon = ["Lost":0,"Won":1]
for (key,value) in whoWon {
if ((plays[6] == value && plays[7] == value && plays[8] == value) || //across the bottom
(plays[3] == value && plays[4] == value && plays[5] == value) || //across the middle
(plays[0] == value && plays[1] == value && plays[2] == value) || //across the top
(plays[6] == value && plays[3] == value && plays[0] == value) || //down the left side
(plays[7] == value && plays[4] == value && plays[1] == value) || //down the middle
(plays[8] == value && plays[5] == value && plays[2] == value) || //down the right side
(plays[6] == value && plays[4] == value && plays[2] == value) || //diagonal
(plays[8] == value && plays[4] == value && plays[0] == value)){//diagonal
userMessage.hidden = false
youLabel.hidden = false
userMessage.text = "\(key)!"
done = true;
}
}
}
To break it up, you just need to add more ()s. Like this:
if (((plays[6] == value) && (plays[7] == value) && (plays[8] == value)) ||
((plays[3] == value) && (plays[4] == value) && (plays[5] == value)) ||
((plays[0] == value) && (plays[1] == value) && (plays[2] == value)) ||
((plays[8] == value) && (plays[4] == value) && (plays[0] == value)))