I want to select rows from multiple tables using subsonic. For one table I can use Query object, but I don't know how I can add more than one tables to query.
You neet to join them, much like you would do in SQL.
If you have a foreign key relationship in the schema, Subsonic is smart enough to figure the joins directly:
DataSet DS = DB.Select().From<Table1>().InnerJoin<Table2>().ExecuteDataSet();
If you don't have a FKI between the tables, you need to manually specify the columns from each table to create the join:
DataSet DS = DB.Select().From<Table1>().InnerJoin(Table1.FKIColumn,Table2.IDColumn).ExecuteDataSet();
Similarly you can create the Left/Right Outer joins,etc.
Remeber you can only join them on simple FKI constraints. For example I found no easy way to do "INNER JOIN Table2 on Table1.FKI = Table2.ID and Table2.CreateDate>Table1.CreateDate" directly from SubSonic.
And a big downside to using SubSonic multiple table joins is that you will run into troubles if you have identically named columns in both tables.
Related
I have a query with multiple joined tables, this is a simple version, I have many such with further levels of joins:
SELECT ApplicationField.OID, ApplicationField.FieldName, ApplicationField.Label, ApplicationField.DataType, ApplicationField.Length, ApplicationField.FieldType, ApplicationField.DefaultValue, ApplicationField.DataPrefix,
ApplicationField.DisplaySequence, ApplicationField.TabNumber, ApplicationField.Required, ApplicationField.Hidden, ApplicationField.PasswordField, ApplicationField.Loopback, ApplicationField.ApplicationVersion,
ApplicationField.OptimisticLockField, FieldQuery.QueryID, FieldQuery.ApplicationField, FieldQuery.ExecuteSequence, FieldQuery.MaxRows, FieldQuery.ReturnField, FieldQuery.QueryType
FROM ApplicationField LEFT OUTER JOIN
FieldQuery ON ApplicationField.OID = FieldQuery.ApplicationField
WHERE(ApplicationField.ApplicationVersion = #versionOID)
ORDER BY ApplicationField.TabNumber, ApplicationField.DisplaySequence
How can I use a Datareader or DataAdapter using a single SQl statement that returns multiple related tables, keeping the tables separate in a dataset, so that I get separate tables for ApplicationFields and related FieldQueries? There may be multiple FieldQueries per Application Field, one to many relationship.
I have a database with multiple tables and some stored procedures that query if to get specific data from various tables in order to display desired information.
Find one example of a stored procedure bellow:
SELECT DISTINCT
ROW_NUMBER() OVER (ORDER BY dbo.[table1].ReportedDate DESC) AS rowNumber,
dbo.[table1].[id],
dbo.[table1].caseReference,
dbo.[table2].organisationName AS customerName,
dbo.[table3].assetRegistration,
dbo.[table4].surname
FROM dbo.[table1] WITH (NOLOCK)
LEFT JOIN dbo.[table2] with (NOLOCK)
ON dbo.[table2].JobId = dbo.[table1].[id]
LEFT JOIN dbo.[table3] WITH (NOLOCK)
ON dbo.[table3].id = dbo.[table2].[JobServiceId]
LEFT JOIN dbo.[table4] WITH (NOLOCK)
ON dbo.[table4].[jobID] = dbo.[table1].[id]
WHERE (table1.caseReference LIKE #caseReference+'%')
I want to move from using such stored procedures to a more code based approach using entity framework. How can I recreate a query like the one above using Linq query over the dbContext classes mapped to the database?
I am mostly having problems in figuring out how to choose the data I want to be returned from each table and how to put it all together.
you can do it easly Linq query or lambda expressions over the dbContext classes mapped to the database.
check this it would be help you
Entity Framework Join 3 Tables
How to join multiple tables?
I am programming an Excel add-in in C# where I process data contained in different DataTable objects. I would like to provide a function to perform SQL queries on the data, with the ability to reference data from other tables in where and sort by clauses (for example, using a join).
An example of such a query would be
SELECT name
FROM Table1
WHERE id = Table2.id AND Table2.age > 18
The problem with this is that a DataTable doesn't know of the existance of the other DataTables, so (for so far I know) there are no such methods in the class. Also, I cannot use something like LINQ, since the query will be written by the users of the add-in in excel.
Would it be a good solution to copy the data to an in-memory database, where each DataTable is mapped to a table? How would this work performance-wise? Is there a simpler solution?
In terms of SQL query you are missing a table reference in selecting the tables, corrected query will look like
SELECT name
FROM Table1, Table2
WHERE Table1.id = Table2.id AND Table2.age > 18
Use Table1.name if there is same named attribute in Table2.
However using only WHERE condition in Joins without specifying the joining attribute is not recommended read this question. Use JOIN.
SELECT Table1.name
FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id WHERE Table2.age > 18
I have dynamic SQL statement generated using criteria. This statement involves multiple joins using many tables. Requirement is to get list of all the tables used in this statement.
For example:
SELECT
table1.a, table2.b, ...
FROM
table1
INNER JOIN
table2 ON table1.col1 = table2.col1
LEFT OUTER JOIN
table3 ON table2.col3 = table3.col1
I want to get list of tables used in the above query dynamically like:
table1,table2,table3.
I am using C# and SQL Server. I think parsing the string and finding individual tables will be complex. Is there any way we can get the list of Tables used in a query from SQL Server itself?
I can find a direct table foreign key in c# like:
foreach (ForeignKey key in CurrentTable.ForeignKeys) {
Console.WriteLine("FK:" + key.ReferencedTable);
}
The classic representation for a many to many relationship is achieved via an intermediate table that has a one to many relation to the two principal tables so there is not a direct link between the tables that have the many2many relation.
For the moment I try to find the many to many relation checking if the table I am analyzing has only two columns and both colums are foreign keys to different tables (in that case I recognise this as the intermediate table).
This is not the best solution as sometimes the intermediate table carries also other columns.
What would the best way to find this many2many relation programmatically?
As additional criteria you could use the fact that in many cases the couple of the two foreign keys represents the primary key of the join/intermediate table.
You seem to have posited your own solution. Rather than searching for tables that have two foreign keys referencing other distinct tables and with no other columns, just delete the phrase "and with no other columns".
In other words, look for "tables that have two foreign keys referencing other distinct tables".
This statement finds all tables with at least two foreign key columns
SELECT objects_1.name AS Master1, objects_2.name AS Master2, sys.objects.name AS Detail,
sys.columns.name AS Column1, columns_1.name AS Column2
FROM sys.objects
INNER JOIN sys.columns ON sys.objects.object_id = sys.columns.object_id
INNER JOIN sys.foreign_key_columns ON sys.columns.column_id = sys.foreign_key_columns.parent_column_id
AND sys.columns.object_id = sys.foreign_key_columns.parent_object_id
INNER JOIN sys.objects AS objects_1 ON sys.foreign_key_columns.referenced_object_id = objects_1.object_id
INNER JOIN sys.columns AS columns_1 ON sys.objects.object_id = columns_1.object_id
AND columns_1.column_id <> sys.columns.column_id
INNER JOIN sys.foreign_key_columns AS foreign_key_columns_1 ON columns_1.object_id = foreign_key_columns_1.parent_object_id
AND columns_1.column_id = foreign_key_columns_1.parent_column_id
INNER JOIN sys.objects AS objects_2 ON objects_2.object_id = foreign_key_columns_1.referenced_object_id
WHERE (sys.columns.is_nullable = 0) AND (columns_1.is_nullable = 0)
ORDER BY Master1, Detail, Master2
If a table has foreign key columns to two different tables, then it definitely represents the many to many relationship between the tables. The fact that the table has other columns only means that the relationship between the tables(entities) is further qualified by the extra attributes. Have a look at the example in the figure below:
In the example above there is a many to many relationship between product and bill. The intersection table will also have descriptive attributes like 'quantity', 'gift wrap' etc.. That does not take away the fact that there is a many to many relationship in the first place. The tables would look like this
Product(prod_id, name, unitweight, uom)
Bill(BillNo, date, amount,uom)
BillItems(prod_Id, BillNo,quantity,gift_flg)