0

I am developing app with MVC 3.5 and EF.

I have written the SQL query and I want to rewrite in the LINQ, but I don't know how to use it...

here is the sql query

select * from PurchaseOrders where CreatedById in
(select Employees_Id from EmployeeRole where Roles_Id in 
(select Roles_Id from EmployeeRole where Employees_Id = 17))

enter image description here

4
  • Need your schema. Honestly do not get what you are trying to do in WHERE clause. Commented Jul 8, 2013 at 10:59
  • 1
    It looks like a strange kind of join... Commented Jul 8, 2013 at 11:00
  • Please check now... I have added the Model Diagram... Commented Jul 8, 2013 at 11:04
  • codeproject.com/Articles/33900/LINQ-FAQ-for-Newbie-s good starting point Commented Jul 8, 2013 at 11:08

3 Answers 3

2

Assuming:-

  1. your context is set up correctly and you have all the navigation properties in place
  2. your query is "Get me all of the purcahse orders created by any employee who shares a role with employee #17"

You can use:-

context.Employees.Where(x => x.Id == 17)
                 .SelectMany(x => x.Roles)
                 .SelectMany(x => x.Employees)
                 .Distinct()
                 .SelectMany(x => x.PurchaseOrders);
Sign up to request clarification or add additional context in comments.

9 Comments

Nice one. But shouldn't you add a Distinct() after SelectMany Employees?
Depends on the behaviour the OP wants, but yeah that sounds most reasonable.
You are genius Iain, I have the same req. but i dont get the x . what is it ?
@user1650894 Is Iain's assumption #2 correct? (This is how i read your sql query, too)
yes it is...2nd assumption is right...but its now working , giving syntax error ...
|
0

Assuming that your above query is just a bit weird and you actually meant to do:

SELECT * FROM PurchaseOrders WHERE CreatedById = 17;

Your LINQ query would be:

PurchaseOrders.Where(Order => Order.CreatedById = 17);

or

var Orders = from Order in PurchaseOrders
             where Order.CreatedById = 17
             select Order;

Seeing your update I guess you would actually be better off selecting you employee and then all of the purchase orders i.e.

var Orders = Employees.Single(x => x.Id == 17).PurchaseOrders;

but beware above will work only if there is such an employee

2 Comments

Why are you involving 3 tables? You only have one parameter and you are not trying to get either user role or the user...
Get me all of the purcahse orders created by any employee who shares a role with employee #17
0
var s= (from po in _db.PurchaseOrders
                join er in _db.EmployeeRoles on po.CreatedById equals er.Employees_Id
                let _ser in _db.EmployeeRoles.Where(c=>c.Employees_Id == 17)
                where  _ser.Select(c=>c.Roles_Id).contails(er.Roles_Id)
        Select po).toList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.