This question is locked. New answers and comments are not allowed.
Hi,
I have been having trouble with some LINQ queries that Telerik can't seem to parse. I have tried several different variations and I have come up with an example that fails every time.
Setup:
public class Person{ public int Id { get; set; } public string Name { get; set; }}public class Teacher : Person{}public partial class FluentModelMetadataSource : FluentMetadataSource{ protected override IList<MappingConfiguration> PrepareMapping() { List<MappingConfiguration> mappingConfigurations = new List<MappingConfiguration>(); var person = new MappingConfiguration<Person>(); person.MapType().ToTable("People"); person.HasDiscriminator().ToColumn("PeopleType"); person.HasProperty(p => p.Id).IsIdentity(KeyGenerator.Autoinc); mappingConfigurations.Add(person); var teacher = new MappingConfiguration<Teacher>(); teacher.MapType().Inheritance(InheritanceStrategy.Flat).ToTable("People"); mappingConfigurations.Add(teacher); return mappingConfigurations; } protected override void SetContainerSettings(MetadataContainer container) { container.Name = "FluentModel"; container.DefaultNamespace = "TelerikProjection"; container.NameGenerator.SourceStrategy = Telerik.OpenAccess.Metadata.NamingSourceStrategy.Property; container.NameGenerator.ResolveReservedWords = false; container.NameGenerator.RemoveCamelCase = false; }}Here is my query:
var query = from t in context.GetAll<Teacher>() where t.Id == id select new { Teacher = t, Person = t as Person };query.ToList()Running the code above, I get the following failure:
An exception occurred during the execution of 'Extent<TelerikProjection.Data.Entities.Teacher>().Where(t => (t.Id == value(TelerikProjection.Program+<>c__DisplayClass1).id)).Select(t => new <>f__AnonymousType0`2(Teacher = t, Person = (t As Person)))'. Failure: Object reference not set to an instance of an object.See InnerException for more details.Complete Expression:.Call System.Linq.Queryable.Select( .Call System.Linq.Queryable.Where( .Constant<Telerik.OpenAccess.Query.ExtentQueryImpl`1[TelerikProjection.Data.Entities.Teacher]>(Extent<TelerikProjection.Data.Entities.Teacher>()), '(.Lambda #Lambda1<System.Func`2[TelerikProjection.Data.Entities.Teacher,System.Boolean]>)), '(.Lambda #Lambda2<System.Func`2[TelerikProjection.Data.Entities.Teacher,<>f__AnonymousType0`2[TelerikProjection.Data.Entities.Teacher,TelerikProjection.Data.Entities.Person]]>)).Lambda #Lambda1<System.Func`2[TelerikProjection.Data.Entities.Teacher,System.Boolean]>(TelerikProjection.Data.Entities.Teacher $t){ $t.Id == .Constant<TelerikProjection.Program+<>c__DisplayClass1>(TelerikProjection.Program+<>c__DisplayClass1).id}.Lambda #Lambda2<System.Func`2[TelerikProjection.Data.Entities.Teacher,<>f__AnonymousType0`2[TelerikProjection.Data.Entities.Teacher,TelerikProjection.Data.Entities.Person]]>(TelerikProjection.Data.Entities.Teacher $t){ .New <>f__AnonymousType0`2[TelerikProjection.Data.Entities.Teacher,TelerikProjection.Data.Entities.Person]( $t, $t .As TelerikProjection.Data.Entities.Person)}Stack Trace: at Telerik.OpenAccess.Query.ExpressionCompiler.PerformDatabaseQuery(Type type, Int32 elementAt, Object[] groupResolutionParamValues, Boolean single, Boolean checkOid) at Telerik.OpenAccess.Query.ExpressionExecution.PerformDatabaseQueryMulti[T](Expression expr, ExecutionSettings settings, Object[] grpVals, Boolean checkOid, QueryOptions options) at Telerik.OpenAccess.Query.Piece`1.ExecuteMultiple() at Telerik.OpenAccess.Query.Piece`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at TelerikProjection.Program.QueryData() in c:\Projects\Personal\TelerikProjection\TelerikProjection\Program.cs:line 109 at TelerikProjection.Program.Main(String[] args) in c:\Projects\Personal\TelerikProjection\TelerikProjection\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()Inner Exception:Object reference not set to an instance of an object.Stack Trace: at Telerik.OpenAccess.Query.ExpressionCompiler.PerformDatabaseQueryImpl(Type resultType, Int32 elementAt, Object[] groupResolutionParamValues, Boolean single, Boolean checkOid) at Telerik.OpenAccess.Query.ExpressionCompiler.PerformDatabaseQuery(Type type, Int32 elementAt, Object[] groupResolutionParamValues, Boolean single, Boolean checkOid)I do not get a failure with these two variants:
var query = from t in context.GetAll<Teacher>() where t.Id == id select new { // Teacher = t, Person = t as Person };var result = query.ToList();var query = from t in context.GetAll<Teacher>() where t.Id == id select new { Teacher = t, // Person = t as Person };var result = query.ToList();Both of these compile and run perfectly.
This is just a very simple breakdown of what I'm actually trying to do (populating complex DTOs), but it demonstrates the problem. Unfortunately, the use of "as" instead of a Cast operation is part of the DTOs I am using, which I do not control. Is there any setting or workaround I could use to allow my first query to run successfully?
Thanks,
Paul