views:

145

answers:

2

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;
}
A: 

Edit:

If you actually need to split the two, this should work:

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

  return from c in items
         from i in availList
         where c.Id = i.Id
         select new { c, i };
}
Yannick M.
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
You also have to remember to change the return type from List<Items> to var because you're now returning a new anonymous type.
Justin Niessner
Does var work as a returntype? Pretty intelligent if it can do inferred typing from within the method body :-)
Yannick M.
It has to be var because he's return a IEnumerable of a anonymous type
Stephan
@Stephan, I got that part. I just haven't seen that construct before, and was surprised that the compiler supports this.
Yannick M.
+1  A: 
public var GetItems(List<AvailableItems> availList)
{
    var result = (from c in dataContext.items.ToList()
                  join a in availList on a.id equals c.id
                  where c.status == "current"
                  select new {c.prop1, a.status};             

    return result;
}
Justin Niessner
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?
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
@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
nm, I had the wrong type.
Thanks I ended up using the AsEnumerable suggestion and it works like a charm.
I would filter out the (c.status == "current") before using Linq-to-objects, since it will probably lessen the workload.
Yannick M.