EntityFramework Linq query - linq

I cannot understand what is wrong with following query:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp;
Should this be same as:
select * from mytable
where Barcode like '%11%' and Description like '%EW%';
If I run this in sql server i get four rows as it should be, but not when I run the linq query
i get 0 rows.
Can please someone help me. It is rather a simple query and it is giving me such a headache. Thank you very much

You forget fetch data, do this:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
Also do not call AsEnumerable soon, use it as below:
var tmp = ctx.mytable.Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();

dbset.AsEnumerable<mytable>()...
Don't do that!
You are causing all your data to get pulled from the database before checking the Where condition.
Other than that, it's not really clear why your query isn't working. Your syntax is correct. I'm guessing the data doesn't actually look like you think it does. Either that, or you're expecting like %EW% to match something in a case-insensitive manner and since the Where clause is being evaluated by LINQ to Objects you're not getting that behavior.

Run a query with only one condition? " c.Barcode.Contains("11") ".
That code should run fine.

Related

laravel return '?' instead of '1' in WHERE clause

$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->toSql();
dd($datas);
I wrote query in "laravel 5.2". I displayed it as sql query with function toSql()
and the result is:
"select * from `els` where `status` = ? order by `created_at` desc limit 4"
you can see that there is ? mark. this is why my query doesn't work. why does it return me ? instead of one. I also tried it like this '1' same result.
You may want to read about bindings if you want to understand what does ? mean.
https://laravel.com/docs/5.3/database#running-queries
If you want to debug and watch queries, you can install this debugbar: https://github.com/barryvdh/laravel-debugbar
? is used for Parameter binding which prevent against SQL injection.
If you want to see the raw SQL query which is executed with parameters values, try the below code:
DB::enableQueryLog();
$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->get();
dd(DB::getQueryLog());
Hope this helps!

Doctrine query not executing

trying to execute a Doctrine query that is invariably not happening:
The code I have:
$q = Doctrine_Query::Create()->select('l.lid')->from('lessons l')->where('l.topic =?','Title of topic')
$result = $q->fetchOne() ;
The funny thing is, this returns the wrong column, that is not l.lid but l.someOtherColumn
So not sure where I am goofing up, your comments and critics are much appreciated.
Thanks!
Try to use Create('table_name t ') instead of ->from() ....

LINQ 2 Entities-query is not working, but why?

everyone! ))
Here is code.
var existingEntities = (from record in globalOne.serviceContext.records_out
where record.timestamp.Date == DateTime.Now.Date
select record ).ToList();
It doesn't work.
Another code:
var existingEntities = (from record in globalOne.serviceContext.records_out
where record.timestamp.Day == DateTime.Now.Day
select record ).ToList();
It does work.
So, problem id in next string:
where record.timestamp.**Date** == DateTime.Now.Date
also won't do
where record.timestamp.Date.Equals(DateTime.Now.Date)
But why? I have no clue. "Timestamp" field is dateTime field in MS SQL SERVER.
And - there is NO records in table.
And I almost forgot - what does it mean - "doesn't work".
App just will not reach the breakpoint after that query(first), without any error, without anything.
Thanks.
You can call record.timestamp.Date because EF can't convert it to required expression tree (then convert it to sql command). In fact EF supports limited number of functions and properties, But for DateTime, EF has some good Canonical functions. You can use them in your case, e.g you can use Day(),Month(),Year() functions to solve your problem (see the link).

Linq To NHibernate: .StartsWith on multiple properties

I'm trying to accomplish the following query (notice .StartsWith):
return (from p in _session.Linq<Profile>()
where (p.Firstname + " " + p.Lastname).StartsWith(wildcard)
select p).ToList();
This throws: could not resolve property: Firstname.Lastname.
If I do this:
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard)
select p).ToList();
Everything is working. How can this be?
Thanks in advance!
The Where Expression does not know how to handle the concatenation of strings. It is trying to make sense of the properties, not the values.
Also, for future reference the StartsWith with the concat and the other one with out would in practice return the same thing.
Is this what you want?
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard) || p.Lastname.StartsWith(wildcard)
select p).ToList();
Update: rewritten answer based on new insights and edited questions.
What's in wildcard and what's the expected output vs. input? If you concat "Abel" + " " + "Braaksma" it will return true for wildcard.StartsWith("Abel") or wildcard.StartsWith("Abel Br") but not wildcard.StartsWith("Braaks"). Do you perhaps mean Contains instead? But this won't solve your error:
The exception you receive seems to come from NHibernate, not from your code. Is it possible that Lastname does not have a correct mapping to the database table? Can you show the stacktrace? Can you access the properties outside of the context of the LINQ statement, but filled with data from the table?

how can I see the inputs to a LINQ query?

I've got a LINQ query that looks like this (at the end):
var query = from myTable0 ...
where myTable1.attributeId == 123 && (bunchaStrings.Contains(myTable1.attributeName)) && myTable2.yesNoValue == 'Y'
When I see the query it turns into this
SELECT ... FROM ... INNER JOIN ... WHERE ... AND (UNICODE([t3].[yesNoValue]) = #p3
So what's happening here is that the value of 'Y' is getting turned into '89' via the UNICODE function. That's all fine, but I'd really like to just be able to see the value of #p3 directly and I can't figure out how to see that value via any methods available from my var.
I would recommend piping the generated SQL out to the output window. There you will be able to see the whole SQL and your parameters values. Then it can also be logged.
Code for it can be found here ->
http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11
Or an easier method (if you've got a handy console around):
MyDataContext context = new MyDataContext()
context.Log = Console.Out
You may also be interested in the LINQ to SQL Visualizer : http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx

Resources