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

#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.

Related

Linq outer joins

I need help to convert the following query to Linq
SELECT c.Code, c.Name
from tblCodes as c
where c.code not in
(select Code from npConsultant where ConsultantName = 'X')
and c.Code < 'AA.0000'
When I try in Linqpad it doesn't seem to understand the into or defaultifempty. Maybe these are inproper methods for what I need to do
Simple answer is use the "let" keyword and generate a sub-query that supports your conditional set for the main entity.
var Objlist= from u in tblCodes
let ces = from ce in npConsultant
select ce.code
where !ces.Contains(u.code)
select u;
try something like that:
var query =
from c in Customers
where !(from n in npConsultant
where n.ConsultantName='X'
select n.Code)
.Contains(c.Code)
&& c.Code < 'AA.0000'
select c.Code, c.Name;
I just do not get how Code can be less than 'AA.0000' ....

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)

Entity Framework LINQ - Subquery with Group By

I am trying to achieve a query which includes a subquery which itself includes grouping.
I based my code from answers to this question
The purpose of the code is to perform a simple de-duplication of the 'person' table based on the email address and return the latest person row.
var innerQuery = (from p in db.Person
join r in db.Registration on p equals r.Person
join e in db.EventDetail on r.EventDetail equals e
where e.Client.ClientID == clientID
group p by p.Email into g
select g.Max(p => p.PersonID));
var query = (from p2 in db.Person where innerQuery.Contains(p2.PersonID) select p2);
When the query is attempted to execute, I get the following error message:
LINQ to Entities does not recognize
the method 'Boolean
Contains[Int32](System.Linq.IQueryable`1[System.Int32],
Int32)' method, and this method cannot
be translated into a store expression.
I have tested the innerquery and it just returns a list of ints as expected, but the query fails with the above message.
Any help greatly appreciated.
Isn't query just a join?
var query = from p2 in db.Person
join iq in innerQuery on p2.PersonID equals iq
select p2;
I'm not sure about = iq part but I don't usually use that syntax sorry - in the other form it would be
.Join(innerQuery, p2 => p2.PersonId, iq => iq, (p2, iq) => p2);
for the join and the select.

How to select a table from highest table use LinQ query

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;

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