LINQ slow even before getting data? - entity-framework

I have following query:
var sw = new Stopwatch();
sw.Start();
var model = new StatusModel();
IQueryable<StatusView> statusViewResult = db.StatusViewList;
statusViewResult = statusViewResult.Where(p =>
(query.ClustersSelected.Count == 0 || query.ClustersSelected.Contains(p.Cluster)) &&
(query.ParkNamesSelected.Count == 0 || query.ParkNamesSelected.Contains(p.ParkName)) &&
(query.TurbineNumbersSelected.Count == 0 || query.TurbineNumbersSelected.Contains(p.TurbineNumber)) &&
(query.ModelNamesSelected.Count == 0 || query.ModelNamesSelected.Contains(p.ModelName)) &&
(query.AlarmLevelSelected.Count == 0 || query.AlarmLevelSelected.Contains(p.AlarmLevel)) &&
(query.ParametersSelected.Count == 0 || query.ParametersSelected.Contains(p.Parameter)) &&
(query.TurbineTypesSelected.Count == 0 || query.TurbineTypesSelected.Contains(p.TurbineType)));
sw.Stop();
var d = sw.Elapsed.TotalMilliseconds;
So I am not doing any ToList() so I shouldn't be querying the database at this moment. However I takes around 2 seconds just for building this query - am I missing something here or is this expected?

Related

Give the results of the following Boolean operations

Assuming that A = True, B = False and C = True.
Give the results of the following Boolean operations
AB + BC’ (A + B’C) + B
(b) ABC’ + (B + AC’)B + A’
in boolean multiply operation is AND (&&) operation
and plus + operation is OR(||) operation
and ' operator measn NOT(!)
so,
AB + BC’ (A + B’C) + B
can be written as
(A && B) || [(B && (!C)) && (A || ((!B) && C))] || B
PUTTING THE VALUE IN THE ABOVE EQUATION
= (1 && 0) || [(0 && (!0)) && (1 || ((!1) && 1))] || 1
= 0 || [(0 && 1 )&& (1 || (0 && 1)] ||1
= 0 || [(0 && 1) && (1 || 0)] ||1
= 0 || [0 && 1] ||1
= 0 || 0 || 1
= 1
I KNOW IT IS HARD TO FOLLOW BUT THE ANSWER WILL BE 1
AND FOLLOWING THE SAME LOGIC THE ANSWER TO THE 2ND STATEMENT WILL ALSO BE 1
IF YOU WANT TO UNDERSTAND HOW THIS WORKS PLEASE READ THE DOCS

Long and detailed AND,OR conditional IF statements on MATLAB

Could you please tell me what my mistake is on the following code:
if ((i>156-9 && i<156+9) && (j>406-9 && j<406+9)) || ((i>684-11 && i<684+11) && (j>274-11 && j<274+11)) || ((i>1066-15 && i<1066+15) && (j>67-15 && j<67+15)) || ((i>1559-15 && i<1559+15) && (j>867-15 && j<867+15)) || ((i>1082-18 && i<1082+18) && (j>740-18 && j<740+18))
plot(j, i, 'r+', 'MarkerSize', 7, 'LineWidth', 5);
end
I'd like to use an if statement which obeys to get in the expression if one of these conditions are fulfilled:
((i>156-9 && i<156+9) && (j>406-9 && j<406+9)) OR
((i>684-11 && i<684+11) && (j>274-11 && j<274+11)) OR
((i>1066-15 && i<1066+15) && (j>67-15 && j<67+15)) OR
((i>1559-15 && i<1559+15) && (j>867-15 && j<867+15)) OR
((i>1082-18 && i<1082+18) && (j>740-18 && j<740+18))
Thanks in advance...
Regards.
I have used your expression in the if as is and used this for this little piece of code.
clc; A=[];
for i=1:2000
for j=1:2000
if ((i>156-9 && i<156+9) && (j>406-9 && j<406+9)) || ((i>684-11 && i<684+11) && (j>274-11 && j<274+11)) || ((i>1066-15 && i<1066+15) && (j>67-15 && j<67+15)) || ((i>1559-15 && i<1559+15) && (j>867-15 && j<867+15)) || ((i>1082-18 && i<1082+18) && (j>740-18 && j<740+18))
A = [A, num2str(i),' ',num2str(j), char(10)];
end
end
end
display(A);
It works for me.

Last elseif statement does not execute

My last elseif statement does not execute even if the conditions are met:
Currency_Exchanage != 'Select...' and all other variables (ETF_Exchanage, Index_Exchanage and Stock_Exchanage) = 'Select...'
Here is the section of code that I am concerned about:
if (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1)
if db == 1 && uni == 1
tickers = gnr_bloomberg; % Analsise Bloomberg natural resources
nrm=1;
elseif db == 1 && uni == 2
tickers = all_bloomberg; % Analsise Bloomberg all
nrm=1;
elseif db == 2 && uni == 1
tickers = gnr_yahoo; % Analsise Yahoo natural resources
nrm=1;
elseif db == 2 && uni == 2
tickers = all_yahoo; % Analsise Yahoo all
nrm=1;
end
else
%Yahoo inputs
if (strcmp(ETF_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from ETF
tickers = ETF_Yahoo(:,1);
Exchanges = ETF_Yahoo(:,2);
Exchange = ETF_Exchanage;
db=2; %Yahoo Selection
elseif (strcmp(Index_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(ETF_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Index
tickers = Index_Yahoo(:,1);
Exchanges = Index_Yahoo(:,2);
Exchange = Index_Exchanage;
db=2;
elseif (strcmp(Stock_Exchanage,'Select...') == 0) && (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Stock
tickers = Stock_Yahoo(:,1);
Exchanges = Stock_Yahoo(:,2);
Exchange = Stock_Exchanage;
db=2;
elseif (strcmp(Currency_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(ETF_Exchanage,'Select...') == 1); %Choose exhanges from Currency
tickers = Currency_Yahoo(:,1);
Exchanges = Currency_Yahoo(:,2);
Exchange = Currency_Exchanage;
db=2;
else
msg = 'Error occurred.\Only one Yahoo input menue must be used!';
error(msg)
end
end
Any Help would be much appropriated, I can't see where I'm going wrong here. I am using Matlab 2013a.
Put a breakpoint at the elseif statement in question and then check in the command window what your condition evaluates to.
If it does not evaluate like expected, check what the individual terms evaluate to.
It is important to actually test what the conditions evaluate to in matlab, rather than only visually comparing the string values.
Usually by that point you should get a rough idea what is wrong.
However in your case we can't do these steps for you because something is off. Your code condensed to the more reasonable minimal example
if 1 && 1 && 1...
1;
disp('I was here')
end
does not even execute in R2014a since the interpreter complains about '...' being an unexpected matlab expression.

How to use '||' in if-else statement in iphone appllication?

if((([txtFldO1.text length] == 0 ) || ([txtFldO2.text length] == 0 ) ||
([txtFldO3.text length] == 0 ) || ([txtFldO4.text length] == 0 ) ||
([txtFldO5.text length] == 0 )) && (([txtFldR1.text length]== 0) ||
([txtFldR2.text length]== 0) || ([txtFldR3.text length]== 0) || ([txtFldR4.text length]== 0) || ([txtFldR5.text length]== 0)))
{
//alert
//return
}
This statement does not work. It does access all of the propeties. Could anyone tell me why?
I need to access one txtFldO and one txtFldR.
To access properties, try SELF
self.txtFld00.text.length
It should be like this.
if([txtFldO1.text length] == 0 || [txtFldO2.text length] == 0 || [txtFldO3.text length] == 0 || [txtFldO4.text length] == 0){
// Code here
}
You don't need to put parentheses before condition. Just put one parentheses like above example.
Hope this help.

Multiple statements in where clause

I have this strange problem. I have a simple search requirements where a user can search a given entitiy (Say customer) based on several search criterias. User may choose to use a criteria or not. The search conditions need to 'AND' all the criteria. So I write code like this (which works)
IQueryable _customer;
_customer = from c in DS.properties
where
(txtCustomerName.Text.Length == 0 || c.name == txtCustomerName.Text)
&& (txtpropcust1.Text.Length == 0 || c.customfield1 == txtpropcust1.Text)
&& (txtpropcust2.Text.Length == 0 || c.customfield2 == txtpropcust2.Text)
&& (txtpropcust3.Text.Length == 0 || c.customfield3 == txtpropcust3.Text)
&& (txtpropcust4.Text.Length == 0 || c.customfield4 == txtpropcust4.Text)
&& (txtpropcust13.Text.Length == 0 || c.customfield13 == txtpropcust13.Text)
select c;
GridView1.DataContext = _customer;
The problem is that if I have 14 where clauses, the EF throws an error- 13 works - 14 does not.
I am using EF+WCF data service in a WPF application. Is there a setting somewhere which limits the number of where clauses?
Thanks
To simplify the resulting query, you could use:
var customers = DS.properties;
if (txtCustomerName.Text.Length > 0)
customers = customers.Where(x => x.name == txtCustomerName.Text);
if (txtpropcust1.Text.Length > 0)
customers = customers.Where(x => x.customfield1 == txtpropcust1.Text);
// etc
_customer = customers;
GridView1.DataContext = _customer;
Note that this will only add SQL where clauses when there's a need for it.