linq-expressions

Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'

痞子三分冷 提交于 2019-12-08 16:18:56
问题 i have one common grid view column filter method that filter grid view record with ColumnName and SearchText wise. here when i operate on nullable int datacolumn there is error thrown from this method like : Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' my method code is : public static IQueryable<T> FilterForColumn<T>(this IQueryable<T> queryable, string colName, string searchText) { if (colName != null &&

variable 'x' of type 'Product' referenced from scope, but it is not defined

ε祈祈猫儿з 提交于 2019-12-08 15:33:18
问题 I have a class named Product in class library project. I am using SubSonic SimpleRepository to persist objects. I have a method as follows in Product class: public static IList<Product> Load(Expression<Func<Product, bool>> expression) { var rep=RepoHelper.GetRepo("ConStr"); var products = rep.Find(expression); return products.ToList(); } I'm calling this function like this: private void BindData() { var list = Product.Load(x => x.Active);//Active is of type bool rptrItems.DataSource = list;

Dynamically add new lambda expressions to create a filter

半城伤御伤魂 提交于 2019-12-08 15:13:00
问题 I need to do some filtering on an ObjectSet to obtain the entities I need by doing this : query = this.ObjectSet.Where(x => x.TypeId == 3); // this is just an example; Later in the code (and before launching the deferred execution) I filter the query again like this : query = query.Where(<another lambda here ...>); That works quite well so far. Here is my problem : The entities contains a DateFrom property and a DateTo property, which are both DataTime types. They represent a period of time .

IQueryable Expression Translation

荒凉一梦 提交于 2019-12-08 04:41:31
问题 I am creating an IQueryable that I want to use for a query passed to entity framework. My repository does not expose queryable. var query = new List<Entity>().AsQueryable().Where(x => x.Property == "argument"); I have a method on my repository that will take in an IQueryable. How do I query my DbSet with the same queryable? I am trying to extract the expression from the queryable to build a new expression for the dbset. Here is what I have so far but it does not work: public IDbSet<TEntity>

Expression(Of Func(Of T)).Body.Member.Name bizarre “$vb$local_” added if used inside a Property Get Accessor

♀尐吖头ヾ 提交于 2019-12-08 04:06:27
I have observed the following bizarre behavior, and want to see if anyone already came across the same. In fact, I did quite a bit of searching, but have not bumped in anything related as yet. It has somehow got quite conventional to supply a reference to a Property name from within a Class to a Method by means of a Lambda expression, instead the name String itself. So: RaisePropertyChanged("myProperty") gets RaisePropertyChanged(() => myProperty) in C# or RaisePropertyChanged(Function() myProperty) in VB .Net. The called Method receives that Lambda expression in a System.Linq.Expressions

Expression(Of Func(Of T)).Body.Member.Name bizarre “$vb$local_” added if used inside a Property Get Accessor

孤人 提交于 2019-12-08 03:22:17
问题 I have observed the following bizarre behavior, and want to see if anyone already came across the same. In fact, I did quite a bit of searching, but have not bumped in anything related as yet. It has somehow got quite conventional to supply a reference to a Property name from within a Class to a Method by means of a Lambda expression, instead the name String itself. So: RaisePropertyChanged("myProperty") gets RaisePropertyChanged(() => myProperty) in C# or RaisePropertyChanged(Function()

Build a specific LINQ expression based on another LINQ expression and a value

£可爱£侵袭症+ 提交于 2019-12-07 22:36:20
问题 If I've got a LINQ expression of the form: Expression<Func<MyClass, string, bool>> filterExpression = (x, filterVal) => x.DisplayName.Contains(filterVal); Is there any way I can get to the expression below? Expression<Func<MyClass, bool>> filter = x => x.DisplayName.Contains("John"); I need to use the second expression in a Linq-to-Entities Where call. 回答1: You need to write an ExpressionVisitor that replaces the ParameterExpression with a ConstantExpression . It would look something like

Member Expression cannot convert to object from nullable decimal

◇◆丶佛笑我妖孽 提交于 2019-12-07 11:40:32
问题 I am working on an MVC project and would like to pass the Html.TextboxFor method the name of a property. This is my viewmodel public class RuleViewModel<T> where T : class, IValidatableObject { private T _rule; public T RuleModel { get { return _rule ?? (_rule = Activator.CreateInstance<T>()); } } public RuleMetadata Metadata { get; set; } public Expression<Func<RuleViewModel<T>, Object>> GetParameterByName(PropertyInfo pi) { var fieldName = Expression.Parameter(typeof(RuleViewModel<T>), "x")

LINQ Expression for Contains

浪尽此生 提交于 2019-12-07 06:28:56
问题 I want to add dynamic expression in linq but facing issues on contains method it is working perfectly for Equal method Problem is i'm getting FilterField dynamically how to replace in query So far i had tried List<int> Ids = new List<int>(); **string filterField ="DEPARTMENT"; ==> Dynamic Field** var eParam = Expression.Parameter(typeof(EmployeeDetail), "e"); var comparison = Expression.Equal(Expression.Property(eParam, filterField), Expression.Convert(Expression.Constant(Ids), Expression

Building a custom predicate to act as a filter using a foreach loop

巧了我就是萌 提交于 2019-12-07 05:52:09
问题 I need to filter a list of documents by passing them to a custom filter that I'm struggling to build dynamically using a foreach loop : var mainPredicate = PredicateBuilder.True<Document>(); // mainPredicate is combined to other filters successfully here ... var innerPredicate = PredicateBuilder.False<Document>(); foreach (var period in periods) { var p = period; Expression<Func<Document, bool>> inPeriod = d => d.Date >= p.DateFrom && d.Date <= p.DateTo; innerPredicate = innerPredicate.Or(d =