how to translate custom method to sql [modified] - DONE
-
I researched on google but couldn't find useful things. I' am using Linq To SQL and having trouble when using custom methods in LTS because custom methods can not be translated into SQL. My problem is; i have a generic class and some generic methods in that class. One of the methods receive a string argument which is a property of an entity. I am using reflection to find property specified by a string argument. And i am getting properties value again via Reflection with PropetyInfo.GetVAlue(...) method. Because this method has no translation into SQL i have the translation excepiton. How can i solve this. I used LINQ trees, however no solution. My code is below. The error comes at the last line "pInfo.GetValue(x, null)" has no translation to SQL. /// /// Summary : Gets entity from persistance storage - database - by the specified primary key value /// /// TPrimaryKey : Type of primary key /// PrimaryKey: The entity's primary key value /// Returns : Retruns the entity whose primary key value equals the specified by PrimaryKey argument public virtual TEntity GetByPrimaryKey(TPrimaryKey PrimaryKey) { // Null checkings if (context == null) throw new ArgumentNullException("DataContext argument can not be null."); System.Data.Linq.Table tableEntity = context.GetTable(); if (tableEntity == null) throw new Exception("There is no type of table specified." + typeof(TEntity).ToString()); // Initializing en example of the Entity Type to find PropertyInfo that is marked with System.Data.Linq.Mapping.ColumnAttribute attribute having value of IsPrimaryKey equals to true TEntity entity = new TEntity(); // Find the column that is marked with System.Data.Linq.Mapping.ColumnAttribute attribute having value of IsPrimaryKey equals to true. System.Reflection.PropertyInfo pInfo = this.FindPrimaryKeyColumnedProperty(entity); // If primary key property can not be found throw exception if (pInfo == null) throw new Exception("Property that is marked with System.Data.Linq.Mapping.ColimnAttribute attribute having value of IsPrimaryKey equals to true can not be found"); // Entities in the collection is compared with PrimaryKey value. If an entity has same primarykey value in its
-
I researched on google but couldn't find useful things. I' am using Linq To SQL and having trouble when using custom methods in LTS because custom methods can not be translated into SQL. My problem is; i have a generic class and some generic methods in that class. One of the methods receive a string argument which is a property of an entity. I am using reflection to find property specified by a string argument. And i am getting properties value again via Reflection with PropetyInfo.GetVAlue(...) method. Because this method has no translation into SQL i have the translation excepiton. How can i solve this. I used LINQ trees, however no solution. My code is below. The error comes at the last line "pInfo.GetValue(x, null)" has no translation to SQL. /// /// Summary : Gets entity from persistance storage - database - by the specified primary key value /// /// TPrimaryKey : Type of primary key /// PrimaryKey: The entity's primary key value /// Returns : Retruns the entity whose primary key value equals the specified by PrimaryKey argument public virtual TEntity GetByPrimaryKey(TPrimaryKey PrimaryKey) { // Null checkings if (context == null) throw new ArgumentNullException("DataContext argument can not be null."); System.Data.Linq.Table tableEntity = context.GetTable(); if (tableEntity == null) throw new Exception("There is no type of table specified." + typeof(TEntity).ToString()); // Initializing en example of the Entity Type to find PropertyInfo that is marked with System.Data.Linq.Mapping.ColumnAttribute attribute having value of IsPrimaryKey equals to true TEntity entity = new TEntity(); // Find the column that is marked with System.Data.Linq.Mapping.ColumnAttribute attribute having value of IsPrimaryKey equals to true. System.Reflection.PropertyInfo pInfo = this.FindPrimaryKeyColumnedProperty(entity); // If primary key property can not be found throw exception if (pInfo == null) throw new Exception("Property that is marked with System.Data.Linq.Mapping.ColimnAttribute attribute having value of IsPrimaryKey equals to true can not be found"); // Entities in the collection is compared with PrimaryKey value. If an entity has same primarykey value in its
i have done it guys with expression. This article helped me http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/8b8f766c-c7a2-4b29-a075-2db80a65a0fe/[^] i replaced the return statement to this ; System.Linq.Expressions.ParameterExpression paramExp = System.Linq.Expressions.Expression.Parameter(typeof(TEntity), "ParamExp"); System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate = System.Linq.Expressions.Expression.Lambda<Func<TEntity, bool>>( System.Linq.Expressions.Expression.Equal( System.Linq.Expressions.Expression.Property(paramExp, pInfo), System.Linq.Expressions.Expression.Constant(PrimaryKey)), paramExp); return context.GetTable<TEntity>().Where(predicate).First();