Subsonic 3 Linq Documentation - orm

I'm started looking at Subsonic just yesterday and having trouble figuring out how to do even the most basic tasks. I've watched the demos for ActiveRecord and SimpleRepository, but they don't fit what we want so I'm trying to use the Linq Templates.
The getting started guide for Linq walks through enough to do a query, but how do I do other things like insert a record and get it's auto-increment ID back?
Is there a reasonably comprehensive guide to using Subsonic Linq somewhere?

Well there is this:
http://subsonicproject.com/docs/Using_AdvancedTemplates
Which I can see is a bit sparse :). It works just like Linq to SQL in most cases in that you need to create "DB". That DB allows you to Insert, Delete, etc for all the objects. You can also do aggregates and so on.
using(var db=new NorthwindDB()){
db.Insert.Into("Name").Values("New Name").Execute();
}
The tools used to interact with the DB follow along with our Simple Query tool:
http://subsonicproject.com/docs/Simple_Query_Tool
If you want more things done for you (like getting the new id back, etc) you should stick with ActiveRecord.
Out of curiosity - what doesn't fit?

Related

Query building in Entity Framework

I have been learning Entity Framework Core for about two weeks. I kind of understand how to use it. But when it comes to querying. The query that'is generated from LINQ looks so crazy. It has so many unnecessary duplicated left joins. I cant post my query here and have people help me to optimise it. But I want to know are there a documentation or some article that I can read to understand how queries are generated from LINQ. And all the ways to optimize one query at LINQ level not talking about indexing and stuff related to database.

FuelPHP ORM and Query builder

I recently started a project based on FuelPHP.
So on this site I will have lot of relations.
My question is, is it a good idea to use the query builder and the ORM?
For example would I use the ORM only for relations, and use the query builder to insert update and delete records in the database.
Or is this not a good idea?
The ORM is fairly powerful; it has Create, Read, Update and Delete (CRUD) functionality. http://docs.fuelphp.com/packages/orm/crud.html built into it so it should be possible to use the ORM for the most part. Saying that, I am currently working on a project where we have elected to use both the ORM and the query builder.
There is a thread on the FuelPHP forums that discusses this: http://fuelphp.com/forums/topics/view/7345
In general not a problem, but you have to take into account that the ORM does result caching (on a per request basis).
So if you run an ORM query to retrieve records, then run a QB query to modify those records, the ORM will still return the unmodified versions. Even if you run the same ORM query again (as it will see the data is cached, and will not run another query to retrieve them).

Is there any framework for parsing a SQL-like query into its component parts?

I'm interested in writing a SQL-like query syntax for a CMS I work with. The idea would be that a CMS query could be written in a SQL-ish syntax, and I would convert that to execute through the CMS API.
There would be no field or table selection, so I need some way to get from this:
SELECT WHERE Something = 'something' AND (SomethingElse != 'something' OR AnotherThing == 'something')
Essentially then, I need some way to get the WHERE clauses grouped correctly based on their parentheticals and AND/ORs.
Is there some framework for doing this? Some example of when it's been done? I don't want to re-invent the wheel here, and I know someone else has to have done this in the past.
The answer is yes, there are many frameworks that work in an analog of SQL and convert to SQL. Linq and various Linq translators are a prime example. Knowing exactly which CMS you're working with, and thus which language and platform you're developing in, would be helpful. Some .NET ORMs that support code queries are:
NHibernate - allows use of a SQL-ish language called HQL in strings, or more code-based query construction using expression lists and Linq.
Linq2SQL - On its way out, but for your simpler applications it should be fine. The framework generates DAO classes that map between tables and your domain objects, and you can use coded Linq queries to work with the classes very much like the real tables.
And of course you can use good ol' vanilla ADO.NET with a string SQL query. This has numerous drawbacks, but if you want to have queries in your code, why not make them real SQL? If you wanted to hide your table structure, you could translate table names before submitting queries, so the SQL contained at the web layer (shudder) won't run against your DB.

LINQ Training: Native SQL Queries to LINQ

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

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#