is this a Sql query or linq - sql

any one know what kind of query is this??
var r = dc.user_details.Where(a => a.username.Equals(u.username) && a.passkey.Equals(u.passkey)).FirstOrDefault();

It is LINQ query. (The Enumerable methods are part of system.linq namespace)
The method "where" is part of "Enumerable Methods".
https://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods(v=vs.110).aspx
Inside "Where" the predicate is a lamda expression. (This is a C# programming concept. Not part of LINQ)
https://msdn.microsoft.com/en-us/library/bb397687.aspx
Again the "FirstOrDefault" is a "Enumerable method", You can find it in the first link.

These are an extension methods that achieves the same as LINQ. It is another way for writing LINQ queries.

This is a linq query which checks the username and passkey and gives the result weather the user name and passkey exist in the table or not.

Related

IQueryable Extension to Generate Temporal Table "AS OF" Queries

Having failed to find a satisfying solution, let me post this here:
We're using NHibernate as our ORM and are just beginning to use Sql Server temporal tables. We therefore need some kind of extension to IQueryable (or the HQL Builder or an InterceptingProvider or something) that will allow us to add the "AS OF" clause to our queries, something like
var results = session.Query<Company>
.Where(c => c.Name == "FogCreek")
.AsOf(DateTime.Today.AddYears(-1));
I've been struggling with it too and gave up as I lost too much time trying to find better approach...
So far I've injected FOR SYSTEM_TIME AS OF into SQL using custom HqlGeneratorForMethod but this gave me invalid SQL expression. So I had to fix it in OnPrepareStatement. This is hacky and not elegant solution but works for most simple cases.
Please look at my solution here and feel free to reply if you find better solution

Late evaluation with NHibernate QueryOver

NHibernate.Linq returns IQueryable giving me late evaluation. Can this also be done with QueryOver?
Update:
I will use it to define lots of queries where only a subset would be used. Therefor Future is not the solution, which would execute them all.
I like the IQueryable (IEnumerable) return type from NHibernate.Linq, that will never execute the query if never used.
Firstly, even QueryOver is just a set of definitions to be later converted to the SQL statement. So until you are working with a reference to a
IQueryOver<Entity, Entity> ab = session.QueryOver<Entity>();
and not calling List<Entity>() ... the execution is deferred. That's also how you can use the Detached queries 16.1. Structure of a Query
QueryOver<Cat> query = QueryOver.Of<Cat>()
.Where(c => c.Name == "Paddy");
Another powerful feature is Future. This represents a very easy way how to put few queries on the stack, and only when first of them is required... all are executed and passed to DB Server as a batch. Read here more: NHibernate Futures
They essentially function as a way to defer query execution to a later
date, at which point NHibernate will have more information about what
the application is supposed to do, and optimize for it accordingly
The biggest difference is that it cannot be returned as IQueryable when using QueryOver
EDIT: Extend on a question update
While IQueryable<TEntiy> could be returned only from a session.Query<TEntity>() and not when using QueryOver, ICriteria, HQL ... the same behavior could not be reached when using QueryOver.

NHibernate 3.0 search with substring

I'm making a search with NHibernate 3.0 IQueryOver, where I have a keyword for a search. I need to search in a string to see if it is part of the string,
Query().Where(e => e.Name.Contains(keyword)).List();
But this does not do the job as expected. How should such a search be performed?
I checked the NHibernate source and the ExpressionProcessor for the QueryOver string like you posted above does not support Contains. The operations it supports are IsLike and IsIn. You could either use IsLike or if you are keen on Contains, use Linq. For example :
(from user in db.Users
where names.Contains(user.Name)
select user);
or
query.Where(person.Name.IsLike("%test%")) //In QueryOver
I am guessing that you got an "Unrecognised method call" exception.
As far as I know (at least, for SQL Server, it doesn't seem to work with SQL Server Compact), NHibernate translates that IQueryable into a case insensitive "like '%keywork%'" where clause. Did you expect a case sensitive search?

load record by predicate/lambda via vb.net linq

im pretty confused about lambdas and actually im not even sure i need them here
what im trying to do here is write a function that will return an object from a certain table with a certain criteria
so lets say i can write
function GetRecord(TableName as string,Criteria as string) as object
'do the linq-stuff
end function
now i dont care if the paremeters are strings or lambdas or whatever, but the end result must be that at runtime i dont know which table and which criteria will be used
as sometimes i need to get a customer record by email and sometimes a product by id etc.
if possible i would prefer returning a list of matching objects and then i would just use .firstordefault when i want 1 (such as by id...)
thank you , as always, for taking the time to read this and answer!
all the best
Have you considered using Dynamic LINQ?
Example:
Parsing an expression tree can be a challenging but rewarding method of solving this issue. I think it's overkill and I'd go with Dynamic Linq as decyclone mentioned.
A benefit of parsing the expression tree, however, is that you can have compile time checking of the submitted criteria.
Here are some articles that helped me.
How to: Implement an Expression Tree Visitor: http://msdn.microsoft.com/en-us/library/bb882521(VS.90).aspx
Building a custom IQueryable Provider: http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx
Walkthrough: Creating an IQueryable LINQ Provider: http://msdn.microsoft.com/en-us/library/bb546158(v=VS.90).aspx
Expression Tree Basics:
http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

Linq to nhibernate string comparison

I have a question on linq to nhibernate. I need to create a query in which I could use string comparison:
session.Linq<User>()
.Where(u => String.Compare(u.Name, givenName) < 0)
.ToList()
Do I understand correctly that this is not supported in linq-to-nhibernate? Is there a way to use string comparison in linq-to-nhibernate?
The new integrated provider in NHibernate 3 allows extending it to support pretty much any construct you want.
Check http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-extension.html for a full example.
Of course, you'll need to think of a reasonable HQL representation of that expression.
This is no longer needed. The lambda expression in the question is now supported.