LINQ Training: Native SQL Queries to LINQ - sql

Can anyone point out some good LINQ training resources. Mostly interested in getting some developers that are very skilled with SQL up to speed using Lambda and LINQ queries. They are struggling with some of their more advanced queries and fall back on ExecuteQuery() to kind of do the LINQ thing. Queries that used to be easy in TSQL but are now very difficult for them with LINQ.

I can't recommend LinqPad more highly. It's a free Windows app that allows you to use Linq interactively against your data source. It includes tutorials, and allows you to query your data source interactively like you would do in a query analyzer. An excellent resource when trying to make the jump form SQL to Linq.
http://www.linqpad.net/

Pro LINQ should have most of the information they need in chapters 12-15.

I would suggest you look at a utility named Linqer (http://www.sqltolinq.com/). It converts T-SQL to Linq queries. I have found it quite useful at times (I have no relationship with the product).
Also, get LinqPad. This is a must. http://www.linqpad.net/
Randy

Related

SQL Query to ER-type diagram

I'm getting started with SQL. So far, most of my experience is either with simple queries using C# (then performing operations on the data in C#), or designing [relatively] more complex queries with MS Access' design view.
I was recently assigned a task to review a relatively large query (hundreds of rows for a single Select), and there are minimal comments in the code. Also, I'm not familiar with the database being queried. Even though the syntax seems relatively simple, there are so many joins on top of joins that it's hard to wrap my head around.
Ideally, I'd like to have some graphical representation, similar to MS Access' design view. Note that my current position uses SSMS. I'm aware there are tools that can create SQL code from a UML diagram. I'm also aware that there are tools that can create a ER diagram from SQL code (see this question), which is closer to what I'm looking for. Are these ER tools my only option? As far as I can tell, the ER tools only show db relationships, and not specific queries with special types of joins or functions.
Thank you.
If you're using SQL Management Studio (I use 2008) you can just right click on the diagrams drop down in the explorer and click Create New Diagram. I'm not sure if this is what you are looking for but this is how I make all of my ER diagrams for my database.
No, It is impossible to do this by using standart tools like SQL Management Studio or others.
Maybe there exist some specific tools for this.
It is difficult and interesting task to generate joins diagram from SQL query (It is simple for "joins" but how does "union" and "where" clauses should be explained on diagram?)
And I think it doesnt simplify you task.

How do I know what does EF translate Linq queries into?

On many sites/blogs I noticed that people, who want to explain linq mechanism, write Linq Queries and provide SQL translation for that. How can I get the translation for my own queries?
I'm working with EF .Net 4.0 in VS2010. Is there a place (property or even some third-party tool) where I can see what they're being translated into?
You can use the ObjectQuery.ToTraceString method:
var query = ...
string sql = ((System.Data.Objects.ObjectQuery)query).ToTraceString();
However, some shortcomings of this approach is that it will only show queries, not updates/inserts/deletes. Also, it won't reveal the parameters used.
If you need as much detail as possible from the query then use the SQL Server Profiler as Neil suggested.
Julie Lerman has a good article with different options on MSDN: Profiling Database Activity in the Entity Framework. She mentions:
ObjectQuery.ToTraceString method
Visual Studio 2010 Ultimate's IntelliTrace
EFTracingProvider
The simplest method is to use SQL Profiler. I have found this a real god-send when it comes to debugging.
You can use LinqPad, you can query your entity model and view the generated SQL.

How to call a L2S provider to translate a predicate in form of Func<T,bool> to a SQL where clause?

I am using L2S in my application but i'm switching more and more to dapper.net because i'm having major issues with query performance and the annoying n+1 selects problem.
Its working fine, but i miss the comfortable LINQish style of writing predicates when filtering data.
So my question is, how can i translate a predicate in form of Func<T, bool> to a SQL Server where clause to use it with dapper?
I think someone must have done this before, or are all the dapper users handcoding their sql statements?
As the title suggests, maybe it is an option to call the LINQ2SQL provider for SQL Server?
Basically i'm looking for something like the inverse of dynamic linq.
There are ways to see the SQL generated by LINQ. See the article Seeing the SQL Generated by LINQ to Entity Queries from Visual Studio Magazine.
Now that I understand better what you're after, I did a little more looking into this and found a couple of related posts that are worth a look:
Dynamic where clause in dapper which suggests using a Stringbuilder, but one of the comments points to a Sam Saffron article, Porting LINQ-2-SQL to Dapper for great justice, that talks about a contributed SqlBuilder that might help you.
Generate a SQL clause using a Linq-to-Sql Expression which suggests using a LINQ Where call and grabbing the WHERE clause from the generated SQL.

SQL Query builder in Delphi

I need to give users the ability to build a simple SQL query against our database. Our application is written in Delphi.
I am assuming only moderate levels of knowledge by the user, but they need the ability to build a simple select statement to be able to query against a couple of tables. If I can make this easy for them, that would be most wondrous.
Does anyone know of a tool or a set of components that I can use to help the users build SQL SELECT statements...
I've used the DevExpress ExpressFilter control to allow users to specify SQL where constraints before.
I've come across FastQueryBuilder http://fast-report.com/en/products/visual-query-builder.html
and
OpenQueryBuilder http://fast-report.com/en/products/free-query-builder.html
(apparently differing versions of the same tool) at Fast-Report.
It looks like the right idea. Anyone have any experience with it?
I've used SimpleQuery for years and am quite happy with it. (http://devtools.korzh.com/eq/vcl/) It's not completely intuitive, but once you get the hang of it, you can present fields to the user from your database(s) and they can combine any series of them with ANDs and ORs to make very complex queries. You get a separate SQL window to save the resulting code. I make The resulting dataset available for printout with PrintDat! or to save as CSV data from the Woll2Woll Infopower grid that I use for display. I occasionally allow for XLS export too with XLSReadWrite. It's all quite automatic at this point. Just take the SimpleQuery example and mold that to your particular needs. You should be able to have something very usable in a day. There is a trial and an example free query builder standalone program that will give you an idea of the final look.
TMS Query Studio? Good value at 75 Euro..
Try EMS Advanced Query Builder.
is a powerful component suite for
Borland® Delphi® and C++ Builder®
intended for visual building SQL
statements for the SELECT, INSERT,
UPDATE and DELETE clauses. It allows
you to build new queries visually
and/or graphically represent the
existing queries in your own
applications. The suite includes
components for working with standard
SQL, MS SQL, InterBase/Firebird,
MySQL, PostgreSQL and many more
dialects. Advanced Query Builder
enables users to make up large and
complicated SQL queries with unions
and subqueries for different servers
without any knowledge of the SQL
syntax.
(source: sqlmanager.net)
(source: sqlmanager.net)
I think the most powerful one is ActiveQueryBuilder, which now included with Delphi IDE (Since 2007 I think)
(source: activequerybuilder.com)
I've written a couple of similar things in Delphi. It's easy enough to allow the user to pick table and column names by querying the metadata and using lists and tree views for display. The difficulty comes when trying to implement things like joins. I've never come up with a good interface for this, and in my experience neither have many of the major data tools players.
You may want to try this free builder:
GSC Visual Query Builder v.0.7
Visual Query Builder v.1.0
and some other in Torry:
http://www.torry.net/pages.php?id=546

Is it a good idea to learn LINQ first, then SQL?

I know that in most cases it`s more useful to learn more complicated technology/language and then easy one than vice versa.
But, actually, time to do university tasks is limited. If I first learn LINQ, then go for SQL, would it be hard for me to use and learn sql?
EDIT
The task I need to do is to work with database and get some data from it, so the question is almost about LINQ to SQL.
It is a bad idea.
And if today's universities teach you LINQ instead of giving you foundation to build your knowledge upon, I can only feel sorry and pity for their students.
Time is always limited. Don't waste it on things that are subject to constant change.
SQL will be there tomorrow, LINQ.... well who knows.
SQL is applicable anywhere, LINQ only in .NET world.
Either LINQ or something else, it will be easy to "learn" it afterwards. When you have the knowledge of SQL, it will just me a matter of hours/days/weeks, hardly longer.
Well, the 2 things are very different. LINQ (in the pure sense) isn't actually related to databases at all - it can be used quite happily just with in memory objects, or against web services, etc.
If you are mainly interested in writing better .NET code, then learn LINQ - but learn it properly - perhaps pick up C# in Depth, for example - which covers it very well in the last few chapters.
If you want to know about databases, then sure: learn SQL (such as TSQL) - but understand the differences. Databases are good if you need to write enterprise software, but not necessarily if you just want to do simple tasks.
edit re edit to the question
If you are just getting simple data in and out of the database, then you probably don't need to know much about SQL. Just use LINQ-to-SQL (or whatever tool), and let the ORM tooling worry about it.
Learn SQL first, then LINQ.
That way you'll understand how LINQ-to-SQL is working behind the scenes, but you'll also know enough to be able to cope when LINQ can't do what you need.
SQL is a standard, learn the standard.
More precisely :
learn database theory
learn CODD algebra
then pick up a "common database", do some tutorials on it, ...
I personnaly really like PostgreSQL tutorial
I submit that you cannot effectively use LINQ unless you have SQL knowledge. If you don't understand, at a minimum, the following, you cannot effectively query a database in any fashion:
select
insert
delete
update
joins
group by
boolean algebra
relational theory
set theory
Learning SQL will give you the concepts you need even if you use LINQ later.
Sql. However you could play with linq pad for a while - it is a freeware and realize that LINQ is a nice hybrid between SQL and C#