Linq's "IN" Operator

c# c#-3.0 entity-framework linq

Question

Here, I'm attempting to convert an outdated raw SQL query to a Linq query using Entity Framework.

It was combining a group of things using the IN operator. The inquiry was along these lines:

SELECT Members.Name
FROM Members
WHERE Members.ID IN ( SELECT DISTINCT ManufacturerID FROM Products WHERE Active = 1)
ORDER BY Members.Name ASC

I am unable to utilize the subquery's response since it returns a collection of strings rather than a single string.String.Contains() method.

I considered carrying out the following:

var activeProducts = (
from products in db.ProductSet
where product.Active == true
select product.ManufacturerID);

after that

var activeMembers = (
from member in db.ContactSet
where member.ID.ToString().Contains(activeProducts));

However, it ends there, claiming that its reasons are flawed. I am unable to choose activeProducts. Since it returns an IQueryable, it is evident that the property is missing, which is why ManufacturerID is used.

I'm attempting to return a list of members who have at least one active product, in short.

Any hints?

[edit]

Here is the whole query code. The second phrase I attempted with the contains didn't appear to work well with Linq.

Server Error in '/' Application. LINQ to Entities does not recognize the method 'Boolean Contains[String](System.Linq.IQueryable``1[System.String], System.String)' method, and this method cannot be translated into a store expression.

    var activeProduct =(from product in Master.DataContext.ProductSet
                        where product.Active == true
                           && product.ShowOnWebSite == true
                           && product.AvailableDate <= DateTime.Today
                           && ( product.DiscontinuationDate == null || product.DiscontinuationDate >= DateTime.Today )
                        select product.ManufacturerID.ToString() );

    var activeArtists = from artist in Master.DataContext.ContactSet
                        where activeProduct.Contains(artist.ID.ToString())
                        select artist;

    NumberOfArtists = activeArtists.Count();

    artistsRepeater.DataSource = activeArtists;
    artistsRepeater.DataBind();

More information Evidently, ManufacturerID is a nullable GUID...

There are no hints here, however for some reason the ContactSet class does not have any references to the goods, therefore I presume I will need to do a join query.

1
13
1/14/2010 5:48:34 PM

Accepted Answer

var activeMembers = (
from member in db.ContactSet
where activeProducts.Select(x=>x.ID).Contains(member.ID));
15
12/24/2012 7:09:32 PM

Popular Answer

Try where activeProducts.Contains(member.ID) .
EDIT: Have you ever tried it without anyToString s?



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow