I am looking for the equivalent of this feature of T-SQL:
SELECT *
FROM dbo.users
WHERE CONTAINS (name, 'Jack')
in Entity Framework.
Thanks.
P.S. "contains" in linq is equivalent of LIKE in TSQL (eg., '%jack%'). I'm not looking for this because this kind of search, especially on large databases may affect the performance.
The CONTAINS keyword is part of the full-text search capability in SQL Server - and as of this day, EF doesn't natively support full-text searching.
There are some approaches out there using EF "interceptors" or plain and simple T-SQL stored procedure to include this functionality into EF - but it's not part of the EF package provided by Microsoft.
See these other SO questions:
Entity Framework, Code First and Full Text Search
How to execute a full text search using entity framework 6)
I think it's just users.Where(x => x.name.Contains("jack"));
In fact, In SQL, it should be
SELECT *
FROM dbo.users
WHERE name like '%Jack%'
And in EF, It equals to
var result = dbContext.users.Where(p => p.name.Contains("Jack"))
Related
Looking at Dynamic Linq, it's possible to use strings to define the key parts of the query. My question is, is it possible to pass the entire query in as a string?
ie: var foo = "from..."
LINQ stands for "Language INtegrated Queries" - it's compiled with the rest of the code, not parsed on the run. You can use Microsoft.CSharp.CSharpCodeProvider to compile your query on the run - but you will have to know in advance what local objects you want to send to the query.
That's not possible in dynamic LINQ -- dynamic LINQ only replaces specific pieces of the query. You can use ExecuteQuery on the data context in LINQ to SQL or SqlQuery on a DbSet<T>, though, to execute specific SQL commands.
I have to Pull all customers whose ids are in the list
I have a list of CustomerID`s
List custidlist=new List{1,2,3....etc.}();
i have to write a linq query to get all customers whose id`s are in the above list
custidlist.
var customers=db.Customers.Where(c=> custidlist.Contains(c.customerid));
Using Contains is not good in performance issue.
Can we use COMPARE OPERATOR LIKE THIS
var customers=db.Customers.Where(c=> custidlist.Compare(c.customerid)); ????
I Heard Compare is best for Performance
Since this is Linq to SQL / Entities your Linq Contains query will be translated to a SQL statement roughly like:
select * from Customers where customerId in (1,2,3)
Not only is your other suggestion not supported, but also you cannot do any better than this SQL performance wise.
When you write a Contains query in Linq to SQL it iwll be fired as an in query in SQL and running your query on the databse should be the fastest..
one caveats to this though is to remember in query might have a limit on the number of entities I think its around 2000+ in sql server and the way around this would be to batch your query.
Hi Experts
I have a special question About dynamic Linq to Sql.
Consider we want to search in a table according two fields*(LetterNo(string) and LetterDate(Datetime))*
.OK the problem is user can enter on of that fields or even both.
I searched in the internet and found "Linq.Dynamic" library in ScottGu weblog.but in that library if we want to use SqlParameter in exported command we should use #0 and param for that.problem is I don't know how many fields user entered.
I want use one query for that and no external tool like "Linq Kit PredicateBuilder".
If I create my query string Manually(and execute using ExecuteCommand) then I will abdicate SqlParameter and risk of Sql Injenction growing up.
How Can do that?
thanks
I suspect you are wanting to do something like the following:
IQueryable<Letter> query = context.Letters;
if (!string.IsNullOrEmpty(LetterNo))
query = query.Where(letter => letter.LetterNo == LetterNo);
If (LetterDate.HasValue)
query = query.Where(letter => letter.LetterDate == LetterDate);
When you execute query, it will combine the necessary expressions and issue a single query to the database based on the user's input.
Consider following LINQ-to-NHibernate queries:
var q1 = from se in query.ToList<SomeEntity>()
where
prop1 == "abc"
select se;
var q2 = from se in q1
where
m1(se.prop2) == "def"
select se;
q2 will not work with error: "The method m1 is not implemented". But when replace q2 with following query, everything goes ok:
var q2 = from se in q1.ToList<SomeEntity>()
where
m1(se.prop2) == "def"
select se;
Why this happens? How can I get first query to work too? Is this something that happens for LINQ-to-NHibernate only or happens in all LINQ queries?
Because there is no way for the LINQ provider to translate the method m1 to a compatible SQL statement.
By calling ToList<SomeEntity>(), you are reading the entire thing into memory and then using LINQ to Objects to filter (and since the query doesn't get translated to SQL in that case, there is no problem running the query).
Unfortunately there is no easy way for you to get the first query to work. If you really need to use m1 to filter results, you'll have to read things into memory first.
This is not just a LINQ to nHibernate limitation either. This will happen in any situation where a LINQ provider uses Expression Trees to convert your code into another language (in this case it is trying to convert your C# code into SQL statements which is the same thing that LINQ to SQL and Entity Framework do).
Presumably the method m1 does not have a translation to SQL (at least, the NHibernate LINQ provider can't figure out how to). When you don't have the ToList, NHibernate is trying to figure out how to convert m1 to SQL. When you do the ToList, NHibernate isn't playing a role anymore and its LINQ-to-Objects that can handle the query. This is specific to ORMs that enable LINQ; LINQ-to-SQL and EF will suffer similar fates.
I would say that your original q2 query is being translated into an expression tree and then when NHibernate tries to parse it, it finds that the method is not a part of its implementation. Converting the query to a collection first with ToList() uses the LINQ functionality of the List which can support the m1 method.
I don't know NHibernate, but would this work ?
var q2 = q1.where (x => m1(x.prop2) == "def");
I thought that the purpose of using Linq2Nibernate was to return IQueryable and build up an expression tree. I am not sure if this is correct or not.
Why would anyone use Linq2Nibernate if they are not going to return IQueryable?
What else would it be used for?
I would love some more input on this topic
Linq For Nhibernate
I'm planning to use NHibernate.Linq to replace my HQL and criteria API queries with LINQ expressions. In other words, I'll generate the query in code (as a LINQ expression) and then pass it to NHibernate.Linq and NHib to convert it into a database query.
FYI there is an alpha version available.
I have planed to start using Linq2Nibernate but haven't got round to i yet.
My reason for wanting to user Linq2Nibernate is the nice syntax when constructing criterions and later querying them out.
Here is a nice simple example.
http://ayende.com/Blog/archive/2007/03/16/Linq-for-NHibernate.aspx
I am using Linq2Nhibernate with my repository pattern that returns IQueryable objects.
As you know, IQueryable is only a query definition - doesn't touch database yet.
Then local requirements are added to the query and finally the object or list is materialized.
Seems to work excellent and prevents unnecessary db queries for partial data at higher abstract layers.
What's Linq2NHibernate? As there are several projects which tried to implement a linq provider for nhibernate but all stopped before reaching completion.
Any linq provider needs to return IQueryable, or better an IEnumerable as that's how linq works. It's convenient to return an IQueryable as you then can re-use existing code to pad additional operators to an already created query (as ctx.Employee which might return IQueryable is already a query)