What is Dynamic Linq Query in dotnet core? - linq

I am working with EF(Dotnet Core) and I need to call data on the basis of Input parameter that is not constant, how can I use if my UI like this, someone told me for doing this kind of work I need to know Dynamic Linq Query.
I need theoretically idea so I can Proceed work for the given situations.

Related

General purpose IQueryable<T> over remote IEnumerable<T>

I have an in-memory dataset of strongly typed objects (~100000) which are accessible through IEnumerable-collections.
Now I would like to query this dataset from another computer.
I can set up a self-hosted WCF-service to open up access to this dataset, but how do I do if I want clients to be able to query this dataset using LINQ?
I know about IQueryable-provider and that they seem very hard to implement.
My question is, are there any general purpose implementation of an IQueryable-provider for my case where a client is supposed to be able to query an in-memory IEnumerable over WCF?
WCF Data Services might be an option but I have not used them for a long time and can not tell how much effort it takes to make them work with an in-memory collection. I can only tell that using them with Entity Framework is quite straight forward but it looks like the reflection data provider should work for you.

Is it possible to use Entity Framework without LINQ?

Is it possible to use Entity Framework without LINQ (linq to entities)?
It's not clear what you mean by using Linq to Entities and EF separately. That's a single library EntityFramework.dll. If you want to write queries on plain SQL, you can do it with SqlQuery() method of DbSet class:
var users = context.Users.SqlQuery("SELECT * FROM dbo.Users").ToList();
In this case EF acts only as mapper, but it does not generates query.
UPDATE According to your comments, you want to avoid referencing Linq. And the answer is - you can't do that, because EF references System.Linq namespace. Consider to use NHibernate with Criteria API.
And remember - Linq is integrated into language. Better spend some time getting around it, than avoid it. Linq is very powerful and you will use it not only for database access, but for everyday working with in-memory collections, xml, datasets and so on.
Yes it possible, but not sure why you would. If you are going to go the route of using queries like this:
var users = context.Users.SqlQuery("SELECT * FROM dbo.Users").ToList();
as suggested above, then why not ditch EF altogether and use something like dapper, which is incredibly easy to use and very fast with a less overhead. Using EF without the linq gives you all the bad parts of of EF (the bloat, slow speeds), without the benefits of the LINQ querying capabilities which makes it so powerful.

ActiveRecord (CDbCriteria) vs QueryBuilder?

I have to make some filters, such as get persons who are in a given department, and I was wondering about the best way to do it.
Some of them are going to require the join of multiple tables.
Does anyone know about the main differences between CDbCriteria and Query Builder? I would particularly like to know about the compatibility with databases.
I found this in the Yii documentation about Query Builder:
It offers certain degree of DB abstraction, which simplifies migration to different DB platforms.
Is it the same for the CDbCriteria objects? Is it better?
The concept of CDbCriteria is used when working with Yii's active record (AR) abstraction (which is usually all of the time). AR requires that you have created models for the various tables in your database.
Query builder a very different way to access the database; in effect it is a structured wrapper that allows you to programmatically construct an SQL query instead of just writing it out as a string (as an added bonus it also offers a degree of database abstraction as you mention).
In a typical application there would be little to no need to use query builder because AR already provides a great deal of functionality and it also offers the same degree of database abstraction.
In some cases you might want to run a very specific type of query that is not convenient or performant to issue through AR. You then have two options:
If the query is fixed or almost fixed then you can simply issue it through DAO; in fact the query builder documentation mentions that "if your queries are simple, it is easier and faster to directly write SQL statements".
If the query needs to be dynamically constructed then query builder becomes a good fit for the job.
So as you can see, query builder is not all that useful most of the time. Only if you want to write very customized and at the same time dynamically constructed queries does it make sense to use it.
The example feature that you mention can and should be implemented using AR.

Dynamic table name in entity framework linq

I am currently using entity framework (.net 4) to read from a 3rd party database using LINQ statements. Unfortunately, at compile time i do not know from which table i will be reading - in fact, new tables can be added to this database after my application is compiled. The table name to read from will be passed as a string parameter to my method.
How should one approach this situation when the table name is not know at compile time? i cannot even add these tables to my data model since they might not yet exist. whilst i like the convenience of linq, i am after a simple approach.
thanks!
For the queries that can only be constructed at run-time and that will return types of different shapes, you're pretty much forced to craft and execute the SQL you want to run yourself. DataContext.ExecuteQuery(string query, params object[] parameters) is going to be your friend.
You probably would already do this, but I would recommend keeping this portion of code isolated to one section of the code, where you execute the query, and then put the results into an strongly typed object before exposing it to other area's of your application. Make sure you clean the table name too.

What is the easiest way to save a LINQ query for later use?

I have a request for a feature to be able to save a user's search for later.
Right now I'm building LINQ statements on the fly based on what the user has specified.
So I started wondering, is there an easy way for me to simply take the query that the user built, and persist it somewhere, preferably my database, so that I can retrieve it later?
Is there some way of persisting the query as XML or perhaps JSON, and then reconstituting the query later?
Never done this before, but I've had this idea:
Rather than having the query run against your database directly, if you were to have it run against an OData endpoint, you could conceivably extract the URL that is generated as the query string, and save that URL for later use. Since OData has a well-though-out spec already, you would be able to profit from other people's labor.
I'd go with a domain-specific object here even if such goodies did exist -- what happens when you save serialized queries in LINQ and your underlying model changes, invalidating everyone's saved queries. Using your own data format should shield you from this to some extent.
Take a look at the Expression class. This will allow you to pre-compile a query. Although persisting this for later use to the DB for better performance is questionable.
I'm writing this as I watch this presentation at PDC10. Just after the 1-hour mark, he shows how he's built a JSON serializer for expression trees. You might find that interesting.

Resources