I've three tables
Investigators: id, name, first name
Articles: id, name, date
ArticlesInvestigators: id_Investigator, id_Articles, order
I want to get only all the investigators from the Investigators table to have one article published:
In SQL it would look like:
SELECT * FROM Investigators i, ArticlesInvestigators a
WHERE i.id = a.id_Articles
But how shall be in LINQ and lambda? Without join because only want the results of investigators table.
I don't know if i understand your question or not but try this way and tell us if it work or not :
1) you need to make FOREIGN KEY between your tables
2) do you query in C#
3) then you can get you data using the relation between your tables
I have the following scenario I need to accomplish via Linq to SQL:
I have a table called JOBS that contains these two columns:
JobID
JobName
I have another table called JOBREFS that contains these two columns:
JobID
JobRefID
I have the following query (I already have the JOBS.JobID)
Select JobRefID from JOBREFS where JOBREFS.JobID = JOBS.JobID
Then I need to do THIS query:
Select JobName from JOBS where JOBS.JobID = JOBREFS.JobRefID
I know I can do this in two queries, but thought there might be a way to get the JobName that is associated with the JobRefID in another table.
Hope this makes sense.
If the navigation properties are correct in your models, you can just use those navigation properties instead of joins. Some examples are here:
http://coding.abel.nu/2012/06/dont-use-linqs-join-navigate/
Hi, I have 3 Tables in my database:
I want show the select Query from this table in treeView. I use RadTreeView, to show in treeview. I need to query by these Fields: DisplyMember | ParentMember | ChildMember.
How do I join theses tables for this query?
Your datasource would be your datagrid or list of whatever you are using to display the table. The display member would be whatever you wanted so say group name for this one. The parent member would be Groups. Value member would be groupid. I'm not real familiar with radtree view but I think this may help. Check this out too. It may help you http://www.telerik.com/help/winforms/treeview-data-binding-binding-to-object-relational-data.html
i used
odbcConnection.Getschema("Columns",new string[]{null,null,"Table1"})
to get the column names of a Table 'Table1'
now how do i specify multiple tables in the restriction in this function call to get list for multiple tables?
and also is there any way to say to get the info for all tables except Table1(not operator)
You could not get the table schema for multiple special tables. You could call
odbcConnection.GetSchema("Columns",new string[]{"catalog1", "owner1"})
to get all tables' schema in catalog1.owner1. And then, you need to find what you want manually.
I am a PHP/MySQL developer, slowly venturing into the realm of C#/SQL Server and I am having a problem in C# when it comes to reading an SQL Server query that joins two tables.
Given the two tables:
TableA:
int:id
VARCHAR(50):name
int:b_id
TableB:
int:id
VARCHAR(50):name
And given the query
SELECT * FROM TableA,TableB WHERE TableA.b_id = TableB.id;
Now in C# I normally read query data in the following fashion:
SqlDataReader data_reader= sql_command.ExecuteReader();
data_reader["Field"];
Except in this case I need to differentiate from TableA's name column, and TableB's name column.
In PHP I would simply ask for the field "TableA.name" or "TableB.name" accordingly but when I try something like
data_reader["TableB.name"];
in C#, my code errors out.
How can fix this? And how can I read a query on multiple tables in C#?
The result set only sees the returned data/column names, not the underlying table. Change your query to something like
SELECT TableA.Name as Name_TA, TableB.Name as Name_TB from ...
Then you can refer to the fields like this:
data_reader["Name_TA"];
To those posting that it is wrong to use "SELECT *", I strongly disagree with you. There are many real world cases where a SELECT * is necessary. Your absolute statements about its "wrong" use may be leading someone astray from what is a legitimate solution.
The problem here does not lie with the use of SELECT *, but with a constraint in ADO.NET.
As the OP points out, in PHP you can index a data row via the "TABLE.COLUMN" syntax, which is also how raw SQL handles column name conflicts:
SELECT table1.ID, table2.ID FROM table1, table;
Why DataReader is not implemented this way I do not know...
That said, a solution to be used could build your SQL statement dynamically by:
querying the schema of the tables you're selecting from
build your SELECT clause by iterating through the column names in the schema
In this way you could build a query like the following without having to know what columns currently exist in the schema for the tables you're selecting from
SELECT TableA.Name as Name_TA, TableB.Name as Name_TB from ...
You could try reading the values by index (a number) rather than by key.
name = data_reader[4];
You will have to experiment to see how the numbers correspond.
Welcome to the real world. In the real world, we don't use "SELECT *". Specify which columns you want, from which tables, and with which alias, if required.
Although it is better to use a column list to remove duplicate columns, if for any reason you want *****, then just use
rdr.item("duplicate_column_name")
This will return the first column value, since the inner join will have the same values in both identical columns, so this will accomplish the task.
Ideally, you should never have duplicate column names, across a database schema. So if you can rename your schema to not have conflicting names.
That rule is for this very situation. Once you've done your join, it is just a new recordset, and generally the table names do go with it.