Asked  7 Months ago    Answers:  5   Viewed   23 times

Is there a tool out there which can convert SQL syntax to LINQ syntax?

I just want to rewrite basic queries with join, etc., to LINQ. It would save me a lot of time.

 Answers

14

Edit 7/17/2020: I cannot delete this accepted answer. It used to be good, but now it isn't. Beware really old posts, guys. I'm removing the link.

[Linqer] is a SQL to LINQ converter tool. It helps you to learn LINQ and convert your existing SQL statements.

Not every SQL statement can be converted to LINQ, but Linqer covers many different types of SQL expressions. Linqer supports both .NET languages - C# and Visual Basic.

Tuesday, June 1, 2021
 
phpmeh
answered 7 Months ago
34

In general there are no tools to covert SQL to Linq as @andres-abel mention before, but sometimes you have to write Linq that will execute exactly as specified SQL (for example because of performance issues, backward compatability or some other reasons).

In this case I'll advice you to do reverse engineering by yourself:

  1. configure logging of dump SQL statements generated by Linq to stdout using
    • ObjectQuery.ToTraceString,
    • DbCommand.CommandText,
    • logger availabe to your data source
  2. manually rewrite Linq statement until you'll get what you need
Wednesday, June 9, 2021
 
michele
answered 6 Months ago
30

Use a custom query

http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx

See: Using the <asp:LinqDataSource> Selecting Event
Wednesday, August 25, 2021
 
ajjumma
answered 4 Months ago
73

TBH in most cases linq or SQL aren't exactly the issue. Your performance will be related to how much data you are inserting, the amount of data currently in your table and the indexes you are maintaining.

Secondly whether you need to do cross checking and/or integrity checks across multiple columns on your data. I have had situations where adding an index and rebuilding a table has taken insert time down from minutes to milliseconds, just due to bad fragmentation and lack of an algorithm.

Linq is an effective way to generate SQL for insertion and modification logic. However you will always end up with the pattern:

  1. Fetch data from database
  2. Modify data using Linq
  3. Submit changes to database.

If you have any logic you can exploit in your insertions, you may be able to use set logic to do updates in SQL. E.g. Update Customers Set KeyCustomer = 1 where Sales > 1000000. The SQL Server will process a command like this 1000s of times faster than you could ever do it with your ORM. However as @gbn has already correctly pointed out, unless you have a team full of strong SQL coders, maintenance will often trump any perf gain in the short term.

If you have to insert a significant number of records, then you should really be looking at batch loading and/or ETL via SSIS. These APIs will use smarter algorithms and perform any constraint checks in batches rather than per insert which will give you excellent performance increases. But managing an SSIS package is far more work than clicking a button in an app. These are all design decisions you will need to consider when you architect your application.

Saturday, September 18, 2021
 
BaajiRao
answered 3 Months ago
21

There's an article here about how to show the SQL being generated which might give a better idea of what SQLite doesn't like: http://msdn.microsoft.com/en-us/library/bb386961.aspx

EDIT for log: From the SqlProvider(Sql2008) part, it looks like it's trying to use SqlServer 2008 syntax instead of SQLite syntax which would explain why it thinks the syntax is invalid.

EDIT again for further info: See this question for info on SQLite and Linq: LINQ with SQLite (linqtosql)

Sunday, November 28, 2021
 
limc
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share