Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Can not Implicitly Convert

Can not Implicitly Convert

Scheduled Pinned Locked Moved LINQ
databasecsharplinqannouncement
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qasim1984
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • Q Qasim1984

      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

      D Offline
      D Offline
      DoctorMick
      wrote on last edited by
      #2

      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.

      Q 1 Reply Last reply
      0
      • D DoctorMick

        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.

        Q Offline
        Q Offline
        Qasim1984
        wrote on last edited by
        #3

        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(); }

        T 1 Reply Last reply
        0
        • Q Qasim1984

          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(); }

          T Offline
          T Offline
          Tauseef A
          wrote on last edited by
          #4

          try returning just simple generic system.collection.generic.list .

          Tauseef A Khan MCP Dotnet framework 2.0.

          Q 1 Reply Last reply
          0
          • T Tauseef A

            try returning just simple generic system.collection.generic.list .

            Tauseef A Khan MCP Dotnet framework 2.0.

            Q Offline
            Q Offline
            Qasim1984
            wrote on last edited by
            #5

            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(); }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups