I am trying to run the code in there selected answer but can’t figure it out
link text
Here is my code:
using System;
using System.Data;
using System.Data.Objects;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace System.Linq.IQueryable
{
static public class LinqExtensions
{
public static IQueryable<TEntity> WhereIn<TEntity, TValue>
(
this ObjectQuery<TEntity> query,
Expression<Func<TEntity, TValue>> selector,
IEnumerable<TValue> collection
)
{
if (selector == null) throw new ArgumentNullException("selector");
if (collection == null) throw new ArgumentNullException("collection");
ParameterExpression p = selector.Parameters.Single();
if (!collection.Any()) return query;
IEnumerable<Expression> equals = collection.Select(value =>
(Expression)Expression.Equal(selector.Body,
Expression.Constant(value, typeof(TValue))));
Expression body = equals.Aggregate((accumulate, equal) =>
Expression.Or(accumulate, equal));
return query.Where(Expression.Lambda<Func<TEntity, bool>>(body, p));
}
}
}
Any ideas
Extensions Methods are tricky in that they are not automatically picked up after you write one. You need either add a using directive or add a reference and then a using directive depending on where you put your extension method.
This is because extension methods are adding to a class, but not in the traditional way of say a partial class. You are extending the class, however, they are not really object-orientated, so it they require more work on your part before you can consume them, confused yet?
The following should help:
personally I would divide them into a Namespace called “ExtensionMethods”, and then further subdivide them into other Namespaces based on purpose, this will help your sanity.
There is no limit to the number of Namespaces you can nest, but there is a limit to how many static classes you can nest.
And in the code that calls it, once you hit the period, you should see it in the auto-complete list.
I will post an example if you need more help.
Example:
Extensions.cs
Usage.cs
Cheers!