1

This may be quite simple, but unfortunately I have a difficulty in figuring out converting an SQL query with an "In" condition to lambda.

SELECT * 
FROM cm_wfapp 
WHERE recgid IN (select distinct wfapp_id from cm_wftrn where approver_id = 32060)

Is there any way to convert this?

5 Answers 5

4

For clearness you can do 2 queries in LINQ. Remeber that LINQ has defered execution model, so actual query will be generated in the moment of actual request.

So you can try to do something like this:

var ids = wfapp_id.Where(i=>i.approver_id == 32060); 

//after use a ids in yor final query. 
var result = cm_wfapp.Where(id=>ids.Contains(id.recgid)); 

This should produce optimal result, but you need to test it agains real DB and its data, to see if LINQ generates good SQL or not.

0
3

This is untested (for obvious reasona), but you could try:

var query = from app in db.wfapp
            where db.wftrn.Where(x => x.approver_id==32060)
                          .Select(x => x.wfapp_id).Contains(app.recgid)
            select app

Let me know whether it works, though.

Alternatively: consider writing it as SQL; most ORms offer a pass-thru mechanism.

5
  • Marc, would that search for x.approver_id == 32060 in wftrn in each iteration ?
    – Habib
    Commented May 16, 2013 at 6:59
  • 1
    @Habib that depends on what the provider is; the OP suggests (by mentioning sql) that it is something like EF/L2S, in which case I would expect that to go down as a single composed TSQL query; and explicitly not to search for the approver_id for each app iteration Commented May 16, 2013 at 7:00
  • 1
    Shouldn't there be a .Select(x => x.wfapp_id) after your where-clause?
    – Dirk
    Commented May 16, 2013 at 7:07
  • @Dirk darn, yes; disadvantage of not being able to test it Commented May 16, 2013 at 7:27
  • To EveryOne: thank so much for the helpful comments, highly appreciate it.. by getting ideas base from the answers that you guys gave,respectively. did run a several trial and error test and i came out to this: app = app.Where(w => db.lxm_wftrns.Any(s => s.appr_id == int.Parse("32060"))); again thanks so much.
    – Von Abanes
    Commented May 16, 2013 at 15:13
1

You can get a List<int> for your sub query like:

List<int> listOfWfaap_id = dbContext.cm_wftrn
                                .Where(r=> r.approver_id == 32060)
                                .ToList();

Then use Contains to check for each ID like:

var result = dbContext.cm_wfapp
                    .Where(r=> listOfWfapp_id.Contains(r=> r.recgid);

You may see: Creating IN Queries With Linq To Sql

1
  • Calling ToList on the early query may be a bad idea if it prevents composition - there could be vast numbers of rows forced over the wire by the first query. It obviously depends on the context, though Commented May 16, 2013 at 6:57
1

If you are using entity framework pre version 6 and .Contains() it is not natively supported:

Native support for Enumerable.Contains

0

You could build your own extension-method like this:

http://geekswithblogs.net/EltonStoneman/archive/2010/07/11/where-in-style-queries-in-linq-to-entities.aspx

which you can use like this:

var result = cm_wfapp.WhereIn(wfapp_id.Where(i=>i.approver_id == 32060));

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.