I tried this query for two tables and it worked, but if I want to do it for many tables how it is done?
cmd.CommandText = "SELECT * FROM assignments
inner join Customers on assignments.Customer_ID = customers.Customer_ID";
//assignments and customers are tables
Consider agents,customer,orders to be your tables & you gotta join them.
SELECT
a.ord_num,
b.cust_name,
a.cust_code,
c.agent_code,
b.cust_city
FROM agents c, customer b, orders a
WHERE b.cust_city = c.working_area
AND a.cust_code = b.cust_code
AND a.agent_code = c.agent_code;
Regards!
Here I am giving one example. You can create queries like this one:
select
*
from tblA a
inner join tblB b
on a.id = b.id
inner join tblC
on a.id = c.id
inner join tblD
on a.id = d.id
If you are using SQL management studio, right click and select "Design Query in Editor". This is the easiest way to join your tables (it's visual)
May be this will help you
SELECT * FROM assignments AS a, customers AS c WHERE a.Customer_ID = c.Customer_ID;
Related
Let's say I have this query in SQL:
SELECT
....
FROM
TableA a
JOIN TableB b on a.Id = b.Id
I've got that working just fine as a linq query:
var results = (from a in db.TableA
join b in db.TableB on a.Id equals b.Id
select new MyObject {...}).ToList();
But now I want to add a left join to the mix as well. In SQL:
SELECT
....
FROM
TableA a
JOIN TableB b on a.Id = b.Id
LEFT JOIN TableC c on a.Id2 = c.Id2
I'm not sure how to handle this in the linq query.
SQL's LEFT JOIN concept is a pretty clunky abstraction when you think about what you're really trying to do. You really want, for every TableA entry, to have a list of TableC items associated with it, right?
LINQ uses a much simpler (IMO) way to represent this abstraction, because it has the concept of nested, hierarchical objects. The provider will generate a LEFT JOIN, but it'll also build the result into an object structure that's easy to work with.
var results = (from a in db.TableA
join b in db.TableB on a.Id equals b.Id
select new MyObject {
...
TableCEntries =
from c in db.TableC
where c.Id == a.Id
select c // or select specific values from c
}).ToList();
If you really want your results flattened out the way they would be with a left outer join, you could do this:
var results = (from a in db.TableA
join b in db.TableB on a.Id equals b.Id
from c in db.TableC
where c.Id == a.Id
select new MyObject {
...
}).ToList();
... but you wouldn't get any entries for TableA where there are no entries present for TableC. For that behavior you'd have to do ugly stuff with group by/DefaultIfEmpty as shown in Ivan Stoev's link.
I have a query that's something like this.
Select a.*
from table1 a
inner join table2 b on a.field1 = b.field1
inner join table3 c on b.field2 = c.field2
where b.field4 = beta and c.field5 = gamma.
On LINQ, I tried to do that this way:
var query = (from a in table1
join b in table2 on a["field1"] equals b["field1"]
join c in table3 on b["field2"] equals c["field2"]
where (b["field4"] == beta && c["field5"] == gamma)
select a).ToList();
But for some reason, when I try to do this I get an error that says that the entity "table2" doesn't have the field Name = "field5", as though as the where clause was all about the last joined table and the other ones were unaccessible. Furthermore, the compiler doesn't seem to notice neither, because it lets me write c["field5"] == gamma with no warning.
Any ideas? Am I writing this wrong?
Thanks
See these links:
How to: Perform Inner Joins (C# Programming Guide)
What is the syntax for an inner join in linq to sql?
Why you don't create View in database, and Select your data from View in LINQ?
I have 3 simple tables (entities): Page (id), Control (id, page_id) and Setting (id, control_id).
So the structure is Page->Control->Setting.
I have query:
SELECT
p.*
,c.*
,s.*
FROM #page p
LEFT JOIN ( SELECT * FROM #control WHERE id = #controlid) c ON p.id = c.page_id
LEFT JOIN ( SELECT * FROM #settings WHERE id = #settingid) s ON s.id = c.page_id
WHERE
p.id = #pageid
How to build NHibernate construction to generate the same query?
If your datamodel is correct you would only have to do something like this
session.QueryOver<Page>()
.Fetch(x => x.Control)
.Eager.List<Page>();
Depending on a lot of thins you may have to implement ThenFetch.
SELECT * FROM register WHERE user_id LIKE 'a%'
SELECT * FROM register WHERE user_id LIKE '%m'
SELECT * FROM register WHERE user_id LIKE '%andru%'
SELECT R.name,C.country_name,S.state_name
FROM register R JOIN country C ON R.country_id=C.country_id
JOIN state S ON R.state_id=S.state_id
SELECT R.name,C.country_name,S.state_name
FROM register R INNER JOIN country C ON R.country_id=C.country_id
INNER JOIN state S ON R.state_id=S.state_id
Now i need LinqToSql Queries instead of these query
var result = context.Registers.Select(x => x.StartsWith(foo)).ToList();
result = context.Registers.Select(x => x.EndsWith(foo)).ToList();
result = context.Registers.Select(x => x.Contains(foo)).ToList();
result = from register in context.Registers
join state in context.States on register.state_id equals state.state_id
select new { register.name, state.country_name, state.state_name }
Note, inner join and join function the same in SQL — thus no need to complicate.
T-SQL:
declare #postlocations table (locationid int)
insert into #postlocations
select locationid
from dbo.PostLocations
where PostId = 162172
select t.*
from dbo.Themes t
inner join dbo.ThemeLocations tl on t.ThemeId = tl.ThemeId
inner join #postlocations pl on tl.LocationId = pl.locationid
LINQ-Entities i have so far:
var postLocations = e.SomePost.Locations; // pre-fetched, e.g materialized ICollection<Post>
var themes = (from t in db.Themes
join q in postLocations on t.Locations.Select(l => l.LocationId) equals q.LocationId
select t).ToList();
But the compiler is complaining on the join keyword about not being able to infer the type arguments.
Any ideas?
I don't think you can join a SQL table with an in-memory list of objects, even if those objects are originally from the database.
Convert the in-memory list of objects to a list of id's (integer), and use that in the join or in a Contains/sub-select. EF can translate the list of id's to parameters when generating the SQL.
The problem with your join is that you're implying a collection of LocationId (t.Locations.Select(l => l.LocationId) can equal a single LocationId. You're trying to join a Theme which has a collection of Locations onto a single Location.
You should be able to fix this by using Contains
var themes = (from t in db.Themes
join q in postLocations
on t.Locations.Select(l => l.LocationId).Contains(q.LocationId)
select t).ToList();
or if EF complains about passing a postLocations as a parameter, you can try
// I'd materialize this but you may not have to
var postLocationIds = postLocations.Select(p => p.LocationId).ToList();
var themes = db.Themes.Where(t => t.Locations.Any(l =>
postLocationIds.Contains(l.LocationId))).ToList();
Edit
how about this
///your sql query
select t.* from dbo.Themes t
inner join dbo.ThemeLocations tl on t.ThemeId = tl.ThemeId
inner join #postlocations pl on tl.LocationId = pl.locationid
//linq query for that
from t in teams
join from tl in teamlocation on t.themid = tl.ThemeID
join from pl in postlocation on tl.temeid = pl.temeid
select t;
Org
Not sure but you can try out by using let keyword
var themes = (from t in db.Themes
let location = t.Locations
join q in postLocations on location.LocationId equals q.LocationId
select t).ToList();