“Tasks” table: “Task History” table: Sql query: Which tasks were completed in the last 30 days? Q: What is the equivalent LINQ expression for this SQL query? Answer This query should do what yo want:
Tag: entity-framework
How to convert SQL query to Linq in Entity Framework 6?
How can I convert this SQL query to a linq expression? Select distinct Clients.Id From Clients Join Orders On Clients.Id = Orders.ClientsId and Orders.Date < DATEADD(DD, -Clients.Term, CAST(…
Selecting multiple columns for updating in Linq
I have a products table which contains thousands of products. I want to update only two columns (price, isAvailable) of this table. So is there is a way to select only those two columns from this table? This is the code that I am using. But I don’t want to select all columns. I have tried this But this is
PostgreSql and EF – Getting ‘duplicate key’ error while doing ‘ToDictionary’ after grouping
Here is my query. UserName column is citext, having non-unique index on it. var logs = Db.Logs .GroupBy(x => x.UserName) .ToDictionary(g => g.Key, g => g.Max(m => m….
How to convert this Entity Framework query to a SQL Server query
I have a query which is written in format like this: How do I convert this query to a plain SQL Server query to see what its output is? Answer In Entity Framework there are a few ways to look at the SQL a query generates. Note: All these ways will use this query: Cast IQueryable to an ObjectQuery and
c# ef core RemoveRange contraint violation
Hi I have a complex db with many tables and relations what we want to do is to copy all tables from a database to another but only taking rows starting from one table (salesorder) and going down following relations. So I avoided the .Include, and use IQueryable queries (one for each table) there is no problem with the insert
FromSqlRaw injection EF Core 3.0
I am wondering how safe the fromSqlRaw method is. I am doing the following in my code, where the person id is a parameter from the method itself: Is this code safe to SQL injection? And is there other security vulnerabilities that I should know of when using this? Answer Use proper parametrization for your input. After clarifications in comments,
How many SQL Queries will be executed in the database through this EF
I have the following and want to know how many queries will be executed in the DB through this EF? private static dbContext dbc = new ProfileDBC(); private static IQueryable GetProfile(…
Why there is no GroupBy clause in internal SQL of Entity Framework linq query?
In documentation of Entity Framework: https://www.entityframeworktutorial.net/querying-entity-graph-in-entity-framework.aspx in section regarding GroupBy we can read that following code: executes internally following SQL: Why there is no GroupBy clause in SQL? If there is no GroupBy clause needed, can’t we just use simple Select with OrderBy and without Joins? Can anyone explain the above query? Answer The bottom line is: because SQL can’t
How to use Group By with Task<IEnumerable> in LINQ
I am new to the entity framework and LINQ. Trying to learn it by example. I have an Entity called “Participant” as below: I am trying to use Group by and return the result as Task<IEnumerable<Participant>>. The Sql Query that I found the is : SELECT Count(Id) as #, Zip FROM [database].[dbo].[Participants] GROUP BY Zip Order By Zip The Code