1

Is this even possible? It seems like I should be able to.

This is my issue. I need to run a web service method from a 3rd party to get a collection of available items where I need the ID and a Status property. Then I have method using LINQ to SQL that retrieves the items that are current.

What I need to do is retrieve the items that are current and available. I can connect them through ID BUT I also need the Status returned from the web service method.

Ideally, my final results will be the same results as from the LINQ to SQL method plus the Status property from the web service method.

Any assistance would be appreciated. Thanks!

Below is what I want to do. Have a method that takes in the list of objects from the web service and join with the existing query.

public List<Items> GetItems(List<AvailableItems> availList)
{
            var result = (from c in dataContext.items
                          where c.status == "current"
                         select c;

             return result;
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536

2 Answers2

1
public List<Item> GetItems(List<AvailableItems> availList)
{
    var result = (from c in dataContext.items.AsEnumerable())
                  join a in availList on a.id equals c.id
                  where c.status == "current"
                  select c;             

    return result.ToList();
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • I can't seem to access the properties in a. It doesn't show up in intellisense. Is there something that needs to be done? –  Oct 02 '09 at 19:02
  • 1
    It's a little cheaper to do .AsEnumerable()- that allows LINQ to SQL to run the query without pre-walking the results, but it usually doesn't make that much difference. – nitzmahone Oct 02 '09 at 19:02
  • @nitzmahone - If you don't pre-walk the results, does LINQ to SQL make a round trip back to the DB for each iteration on the join? – Justin Niessner Oct 02 '09 at 19:18
  • Thanks I ended up using the AsEnumerable suggestion and it works like a charm. –  Oct 02 '09 at 23:13
  • I would filter out the (c.status == "current") before using Linq-to-objects, since it will probably lessen the workload. – Yannick Motton Oct 03 '09 at 01:26
  • You can't have a var return type. This is not a valid C# code. Use IQueryable or IEnumerable if you need deferred execution: http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet – Zar Shardan Nov 23 '12 at 02:36
  • @ZarShardan - Your are correct. Edited the syntax to be correct. Also matched the method signature in the original question rather than your suggestion to substitute IEnumerable or IQueryable. – Justin Niessner Nov 23 '12 at 05:25
0

Re-Edit:

I typically extract the ids as a lookup list, and do the following:

public List<Items> GetItems(List<AvailableItems> availList)
{
  var availableItemIds = availList.Select(a => a.Id).ToList();
  var items = (from item in dataContext.items
               where availableItemIds.Contains(item.Id)
               where item.status == "current"

  return items;
}
Yannick Motton
  • 34,761
  • 4
  • 39
  • 55
  • This won't work without a "barrier" to tell LINQ to SQL to realize the query (see Justin's solution below- the "ToList" is key). – nitzmahone Oct 02 '09 at 19:00
  • You also have to remember to change the return type from List to var because you're now returning a new anonymous type. – Justin Niessner Oct 02 '09 at 19:19
  • Does var work as a returntype? Pretty intelligent if it can do inferred typing from within the method body :-) – Yannick Motton Oct 02 '09 at 20:24
  • It has to be var because he's return a IEnumerable of a anonymous type – Stephan Oct 02 '09 at 20:44
  • @Stephan, I got that part. I just haven't seen that construct before, and was surprised that the compiler supports this. – Yannick Motton Oct 02 '09 at 22:01
  • The compiler does not support this, that's why you're surprised @YannickMotton. "The contextual keyword 'var' may only appear within a local variable declaration". – Stephen Kennedy Oct 19 '14 at 13:06