tags:

views:

28

answers:

1

In the following code, value is a string with comma separated values each of which is a type name. I want to get the entities which have been linked with any of these types or having one of these types in their description. I have written the following linq to sql servery and I am always getting 0 when there certainly entities with these types. Can you please help?

string[] allValues = value.Split(',');

var allTypes = ModelFactory.GetRepository<IRestaurantTypeRepository>().GetAllTypes();

return from ent in matchingEntities
       from typeLink in ent.EntityRestaurantTypeLinks
       join type in allTypes on typeLink.RestaurantTypeId equals type.RestaurantTypeId
       where allValues.Contains(typeLink.RestaurantType.TypeName)
       || (
            allValues.Contains(type.TypeName)
            && ent.Description.Contains(type.TypeName)
           )
       select ent;

EDIT

I have split the statement into two. After that the second statement became like this.

from ent in matchingEntities
                       where allValues.AsEnumerable().Any(va =>    
                       ent.Description.Contains(va))
                       select ent;

In this, allValues is string[] type. ent.Description is string content. I just want to get the entities whose description contains any of the value from allValues. The above statement is not giving results.

+1  A: 

From your description of the problem, it sounds like you mean or, instead of and:

return from ent in matchingEntities
       from typeLink in ent.EntityRestaurantTypeLinks
       join type in allTypes on typeLink.RestaurantTypeId equals type.RestaurantTypeId
       where allValues.Contains(typeLink.RestaurantType.TypeName)
           || allValues.Contains(type.TypeName)
           || ent.Description.Contains(type.TypeName)
       select ent;

Otherwise, the query you have now returns "all entities which have been linked with any of these types or (is one of these types and has one of these types in their description)"

lc
@lc, I have tried your solution and am still getting 0 results.
mohang
@mohang, Are you maybe running into case-sensitivity problems then?I see it, it ought to work. Also, you've checked `allValues` at runtime and the entities are being returned ok if you don't filter with a where clause?
lc