Can not Implicitly Convert
-
Hi all I am new to entity framework and LINQ I have three entities 1. Category 2.Definitions 3.Versions I want to get latest version for each definition by Category.ID Private IList<DynamicForms.Data.Version> getLatestVersion(Guid CategoryID) { DataEntities db = new DataEntities(); IList query = null; Query=(from c in db.Definitions join cv in db.Versions on new { ID = c.ID } equals new { ID = new Guid(cv.DefinitionReference.Value.ID.ToString()) } where c.CategoryReference.Value.ID == CategoryID group new { c, cv } by new { c.ID, c.Modified, cv.Name, cv.OutputTemplate, cv.PageType, cv.State } into g select new { g.Key.ID, g.Key.Name, g.Key.OutputTemplate, g.Key.PageType, g.Key.State, MaxVersion = g.Max(p => p.cv.Number) }).ToList(); return query; } I have following exception Cannot implicitly convert type ‘System.Collections.Generic.List<’Anonomous type #1’> ‘to System.Collection. Generic.Ilist<DynamicForm.Data.Version>. An explicit conversion exist (Are you missing a cast) I am waiting your reply please
-
Hi all I am new to entity framework and LINQ I have three entities 1. Category 2.Definitions 3.Versions I want to get latest version for each definition by Category.ID Private IList<DynamicForms.Data.Version> getLatestVersion(Guid CategoryID) { DataEntities db = new DataEntities(); IList query = null; Query=(from c in db.Definitions join cv in db.Versions on new { ID = c.ID } equals new { ID = new Guid(cv.DefinitionReference.Value.ID.ToString()) } where c.CategoryReference.Value.ID == CategoryID group new { c, cv } by new { c.ID, c.Modified, cv.Name, cv.OutputTemplate, cv.PageType, cv.State } into g select new { g.Key.ID, g.Key.Name, g.Key.OutputTemplate, g.Key.PageType, g.Key.State, MaxVersion = g.Max(p => p.cv.Number) }).ToList(); return query; } I have following exception Cannot implicitly convert type ‘System.Collections.Generic.List<’Anonomous type #1’> ‘to System.Collection. Generic.Ilist<DynamicForm.Data.Version>. An explicit conversion exist (Are you missing a cast) I am waiting your reply please
where you have select new { ... use select new DynamicForms.Data.Version() { ... Using select new without defining the type will create an anonymous type which you won't be able to convert to DynamicForms.Data.Version.
-
where you have select new { ... use select new DynamicForms.Data.Version() { ... Using select new without defining the type will create an anonymous type which you won't be able to convert to DynamicForms.Data.Version.
Thanks for reply I have changed my code and used var query=(form c in ....................) It removed the exception i got Now I receive following exception base {System.SystemException} = {"Only parameterless constructors and initializers are supported in LINQ to Entities."} My modified code is given below private void getLatestVersion(GridView Grid, Guid CategoryID) { DataEntities db = new DataEntities(); var query = (from c in db.Definitions.Include("Versions") join cv in db.Versions on new { ID = c.ID } equals new { ID = new Guid(cv.DefinitionReference.Value.ID.ToString()) } where c.CategoryReference.Value.ID == CategoryID group new { c, cv } by new { c.ID, c.Modified, cv.Name, cv.OutputTemplate, cv.PageType, cv.State } into g select new { g.Key.ID, g.Key.Name, g.Key.OutputTemplate, g.Key.PageType, g.Key.State, MaxVersion = g.Max(p => p.cv.Number) }); Grid.DataSource = query; Grid.DataBind(); }
-
Thanks for reply I have changed my code and used var query=(form c in ....................) It removed the exception i got Now I receive following exception base {System.SystemException} = {"Only parameterless constructors and initializers are supported in LINQ to Entities."} My modified code is given below private void getLatestVersion(GridView Grid, Guid CategoryID) { DataEntities db = new DataEntities(); var query = (from c in db.Definitions.Include("Versions") join cv in db.Versions on new { ID = c.ID } equals new { ID = new Guid(cv.DefinitionReference.Value.ID.ToString()) } where c.CategoryReference.Value.ID == CategoryID group new { c, cv } by new { c.ID, c.Modified, cv.Name, cv.OutputTemplate, cv.PageType, cv.State } into g select new { g.Key.ID, g.Key.Name, g.Key.OutputTemplate, g.Key.PageType, g.Key.State, MaxVersion = g.Max(p => p.cv.Number) }); Grid.DataSource = query; Grid.DataBind(); }
-
try returning just simple generic system.collection.generic.list .
Tauseef A Khan MCP Dotnet framework 2.0.
Thank you Ghumman Sahib Actually I was try to get data in way that is not supported by LINQ query Now I have changed my code to following It is working DataEntities data = new DataEntities(); Category cat = (from c in data.Categorys where c.ID == CategoryID select c).First(); IList<Definition> defintions = (from d in data.Definitions.Include("Versions").Include("Versions.CheckedOutTo") where d.Category.ID == cat.ID select d).ToList(); IList<DynamicForms.Data.Version> versions=null; List<Guid> Ids = new List<Guid>(); if (defintions.Count > 0) { versions = defintions.SelectMany(d => d.Versions).ToList(); var Latestversions = from v in data.Versions.Include("Definition") group v by v.Definition.ID into g select new { DefinitionID = g.Key, LatestVersion = g.Max(v => v.Number) }; versions = (from vers in versions from lvers in Latestversions where vers.Definition.ID == lvers.DefinitionID && vers.Number == lvers.LatestVersion select vers).ToList(); Grid.DataSource = versions; Grid.DataBind(); }