Reputation: 4164
I'm trying to write some linq which searches for items where the value in the stored item matches any of the values in a list - I can do this easily if it's a list of strings & searching for a string, but when it's a list of enums (ints) I can't figure it out.
List<MyEnum> categories = new List<MyEnum>();
categories.Add(MyEnum.Fred);
categories.Add(MyEnum.Bill);
categories.Add(MyEnum.Jim);
// MyTable.Category is of type MyEnum
return (from o in Table<MyTable>()
where categories.Any(val => o.Category.Contains(val))
select o).Count();
Edited with further info -
The code is:
public int LocalOffersCount(double distance, List<LocalCategory> categories)
{
try
{
return (from o in Table<LocalOffer>() where categories.Any(val => o.Category.Contains(val)) select o).Count();
}
catch (Exception ex)
{
Log.Error("DatabaseService->LocalOffersCount: " + ex.Message);
return 0;
}
}
MyTable contains various items, the "Category" item is a LocalCategory enum starting at 1 going to 10.
The compile error I'm getting is -
The type arguments for method `System.Linq.Enumerable.Contains(this System.Collections.Generic.IEnumerable, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly (CS0411)
Upvotes: 2
Views: 11489
Reputation: 148990
My guess is that LocalOffer.Category
is a single Category
(based on the name). Try this instead:
return
(from o in Table<LocalOffer>()
where categories.Contains(o.CategoryId)
select o)
.Count();
However, if LocalOffer.Category
is an IEnumerable<Category>
, this is probably what you need:
return
(from o in Table<LocalOffer>()
where o.Any(c => categories.Contains(c.CategoryId))
select o)
.Count();
Upvotes: 5