Can anyone provide an example of how to join across two schemas using subsonic 2.2.
This doesn't work:
SqlQuery qu = new Select("*")
.From(NorthwindLeads.Lead.Schema)
.InnerJoin(Northwind.StatsMap.SourceIdColumn, NorthwindLeads.Lead.SourceIdColumn);
There's no easy way to do this in subsonic that I'm aware of. I would recommend adding a view to your database which returns the data you want from the other schema and then joining to the view in your subsonic query.
Related
I have two tables in my database in a 1:N relation and I would like to do a left join query with eager loading.
My tables are:
Videos (IDVIdeo, Name... )
Versions (IDVersion, IDVideo, Name, Avaliable...)
Well, in a video I can have many versions (DVD, Blu-Ray... etc) and a version only can belong to a video.
I would like to get all the videos which I have at least one available version (perhaps some version are in possession of one friend).
I would like to get all the videos that have at least avaliable version, but of this videos I want all the versions, avaliable and not avaliable.
So the first step is to know all the videos that have at least ona avaliable version and the second step is to get all the videos and all their versions (avaliable and not avaliable).
I would like to that with raw sql but how it is not possible to use eager loading with raw sql, I would like to use linq.
I want to use eager loading to use only one query to the database and not many, and because I would like to populate the collection versions in the video entity with its versions.
Thanks.
The solution using LINQ is rather straight-forward and would be:
var videos = context.Videos
.Include(v => v.Versions)
.Where(v => v.Versions.Any(vers => vers.Available))
.ToList();
If you really prefer raw SQL for this you can extract the SQL from this LINQ query:
var sql = context.Videos
.Include(v => v.Versions)
.Where(v => v.Versions.Any(vers => vers.Available))
.ToString();
Edit
Most likely a query with raw sql won't populate navigation properties, so it doesn't seem to be possible to use it for eager loading. See the discussion here, in the answer and its comments.
I have found out that SubSonic SimpleRepository expectes plural table names however I have started working on a database that doesn't have this and I am unable to change the table names.
Is there a way I can use Subsonic without making database changes?
I have seen one suggestion but I don't fancy that too much.
I'm not tied to using the SimpleRepository I just thought it would be easiest as I need the ability to swap database connections (SQL & Oracle) based on the clients requirements. The schema is the same on both. With SimpleRepository I can just swap out the connection string in the web.config.
You can apply the SubSonicTableNameOverride attribute on your classes you use with Simple Repo and use an arbitrary table name!
How do I write a SubSonic query similar to this is C#.net:
SELECT * FROM Users WHERE substr(last_name,1,1) = 'S';
I don't want to use "LIKE" it eats up performance.
Don't think you can do this, you can use subsonic to execute the query itself though.
How to here:
http://subsonicproject.com/docs/CodingHorror
I want to no if there any way to add an "Having" constrain to an aggregation select?
Example: if i need all sales sum by date having the sales sum > 1000.
Best Regards,
TheGodfather
SubSonic does have a "having" but you don't explicitly state it.
It is determined from you selecting an Aggregate and adding the Aggregate to the Where clause.
For example (paraphrased from SubSonic AggregateTests.cs)
SubSonic.SqlQuery q = new
Select(Aggregate.GroupBy("ProductID"), Aggregate.Avg("UnitPrice"))
.From("Order Details")
.Where(Aggregate.Avg("UnitPrice"))
.IsGreaterThan(50);
The SubSonic query above will create a SQL statement with a "HAVING AVG(UnitPrice) > 50"
Are you using SubSonic 3.0.0.3 or 2.2?
If you're using 2.2, then I don't think you can do it. I'm unsure about 3.0.
I'd like to add a query to my SubSonic DAL that uses the MySQL fulltext search construct "WHERE MATCH (columnlist) AGAINST (searchterm)", but can't find an equivalent in SubSonic.
Is there a way of using Subsonic to execute a "literal" query - i.e. it just queries with the exact MySQl code I feed it?
Alternatively, could I implement a subclass of SubSonic.Where to run the fulltext search? If so, how would I go about this?
I'm using SubSonic 2.x. Any ideas welcome. Thanks.
ddoctor
You can use CodingHorror to run any SQL you like.