How to select a table from highest table use LinQ query - linq

I have a sequence relationship:
A has many Bs.
B has many Cs.
C has many Ds.
They also make me so confused if there are more than 3 or 4,..tables.
So, how can i select all Cs that satify A.Id="1".
(something likes finding all grandsons of a grandfather)
Thanks in advance.

var x = from a in aArray
from b in a.bArray
from c in b.cArray
where a.id == "1"
select c;

I assume that you are using Linq-to-sql since you mention "table" in the topic.
var query = from c in context.C
where c.b.a.id == "1"
select c;

Related

How to associate three tables?

I have three tables ABC. I hope to display all records of A while linking BC. Field a1 of A is associated with field b1 of B. a1 may be empty, so I wrote
A.a1=B.b1(+)
The two fields c and d of table C are associated with A.a2 and B.b2 respectively
so i wrote
A.a2 = C.c(+) and B.b2 = C.d(+)
The total sql is as follows
select A.a1, A.a2, B.e,C.f,
from A, B, C
where A.a1=B.b1(+)
and A.a2 = C.c(+)
and B.b2 = C.d(+)
But the prompt says that a table can only have one external link at most.
I tried to use case when to display the information of B and C,
select A.a1, A.a2,
case when a1 is null then null
else (selece B.e from B
where B.b1=A.a1) end,
case when a1 is null then null
when (selece B.e from B
where B.b1=A.a1) is null then null
else (selece C.f from C
where C.c=A.a2 and C.d=B.b2) end
from A
but is there any other better association method?
"Older" (I believe lower than 21c) Oracle database versions won't let you outer join one table to two or more other tables using the "old" Oracle's (+) outer join operator.
But, if you switch to JOINs, then you won't have that problem. Something like this:
select *
from b left join a on a.a1 = b.b1
left join c on c.d = b.b2 and c.c = a.a2
(Fetch columns you want, include conditions you need, but - that's the general idea.)

how to write linq to sql with lambda for this sql query

hi friends please help me out to get out of this um new to linq with lambda
select cn from color,related
where cid in (select ciid from related where iid=2)
Without knowing how the relations are set up, the best I can do is free hand, something like;
(from c in db.color
from r in db.related
where c.cid == r.ciid && r.iid == 2
select c).Distinct();
or
from c in db.color
where (from r in db.related where r.iid==2 select r.ciid).Contains(c.cid)
select c;
Try Below expression
(from i in dbcontext.color
where (from j in dbcontext.color where j.iid==2 select j.ciid).contains(i.cid)
select i)

beginner to linq to entity sql: How do I write this linq to Entity query

#Parameter = (some_value)
select * from C1
where C1.number =
(select number from C2
where id = #Parameter)
pointers to good resources for learn the concepts of linq to entities will also be helpful.
Thanks
You can use the following format:
var p = parameter;
from c in C1
where C.number == ( from x in C2
where x.id == p
select x).FirstOrDefault()
select c;
Here is the official MSDN documentation with examples for Linq To Entities.

Difference between the LINQ join and sub from

Is there any difference between these two LINQ statements:
var query = from a in entities.A
where a.Name == "Something"
from b in entities.B
where a.Id == b.Id
select b;
var query = from a in entities.A
join b in entities.B on a.Id equals b.Id
where a.Name == "Something"
select b;
Are both statements doing an inner join?
Also how do I view generated the generated SQL statement from the Entity Framework?
This doesn't precisely answer your question, but it's nearly always wrong to use join in LINQ to Entities. Both queries are, in my opinion, incorrect. What you actually want to do in this case is:
var query = from a in entities.A
where a.Name == "Something"
from b in a.Bs // where Bs is the name of the relationship to B on A,
select b; // whatever it's called
You already have the specification of the relationship encoded in your DB foreign keys and your entity model. Don't duplicate it in your queries.
You can get, and compare the SQL for, those queries:
((ObjectQuery)query).ToTraceString();
The generated SQL may be (subtly) different depending on how EF interprets those queries.
FYI- You don't have to include joins when querying related entities.
Logically speaking these two statements are doing the same thing. If they are computed differently by the framework then I would be unimpressed.
Take a look to the sql profiler. You could get your answer.

Join in linq: "The specified LINQ expression contains references to queries that are associated with different contexts."

Is it possible to make a join in linq and only return data from one dataset where the other key was present, a little like:
var q = from c in customers
join o in orders on c.Key equals o.Key
select new {c.Name, o.OrderNumber};
and then instead of returning just the two records then returning customers like:
var q = from c in customers
join o in orders on c.Key equals o.Key
select c;
When I try to do (something similar) I get this error:
The specified LINQ expression contains references to queries that are associated with different contexts.
I going to assume that you've skipped a Where clause which involved the orders table (or otherwise the join would be pointless)
In which case, you can just have Linq infer the join.
var q = from c in customers
where c.Orders.Any(o=> o.ProductCode == productCode)
select c;
Linq2Sql will automatically create the Orders property if you have a foreign key defined; I believe with the Entity Framework, you have to manually specify it.
The error indicates an other problem:
You have to use the same DataContext on every object in the query if you're using Linq to SQL.
Your code should look somehow like that:
using (MyDataContext dc = new MyDataContext())
{
var q = from c in dc.customers
join o in dc.orders on c.Key equals o.Key
select c;
// process q within the DataContext
// the easies would be to call q.ToList()
}
Will it be in EF 4.0 to create join from multiple context?
For example:
TestModelEntities e1 = new TestModelEntities();
TestModelEntities e2 = new TestModelEntities();
var d = from firme1 in e1.firme
join g in e2.grad on firme1.grad.grad_id equals g.grad_id
select firme1;

Resources