none
EF 4.1 The entity or complex type cannot be constructed in a LINQ to Entities query. RRS feed

  • Question

  • I'm getting this error "The entity or complex type  cannot be constructed in a LINQ to Entities query.".

     

    Why can't I use this code below? 

     

     public IList<Model> GetTherapist()

            {          

                return (from t in dbContext.Table1

                       select new Model() {

                           Id = t.Id,

                           Name = t.Name

                       }).ToList();

            }

    -----

    Do we really have to do like that? 

     

     public IList<Model> GetTherapist()

            {   

                return (from t in ((from t1 in dbContext.Table1

                        select t1 ).ToList())

                        select new Model() {

                           Id = t.Id,

                           Name = t.Name

                       }).ToList();

            }

     


    Michael Sync: blog: http://michaelsync.net
    • Edited by Michael Sync Tuesday, September 27, 2011 10:29 AM
    Tuesday, September 27, 2011 10:24 AM

Answers

  • Hi Michael,

    Welcome!

    I think you can use Anonymous Type to solve your problem:

      var list=(from t in dbContext.Table1
                       select new {
                           Id = t.Id,
                           Name = t.Name
                       }).ToList();
    // then loop the list to tranform it as the type what you want

    You workaround should be like this:

    return (from t  in dbContext.Table1.ToList()
                        select new Model() {
                           Id = t.Id,
                           Name = t.Name
                       }).ToList();
    

    Have a ncie day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Michael Sync Thursday, September 29, 2011 2:20 AM
    Wednesday, September 28, 2011 3:53 AM
    Moderator

All replies

  • Hi Michael,

    Welcome!

    I think you can use Anonymous Type to solve your problem:

      var list=(from t in dbContext.Table1
                       select new {
                           Id = t.Id,
                           Name = t.Name
                       }).ToList();
    // then loop the list to tranform it as the type what you want

    You workaround should be like this:

    return (from t  in dbContext.Table1.ToList()
                        select new Model() {
                           Id = t.Id,
                           Name = t.Name
                       }).ToList();
    

    Have a ncie day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Michael Sync Thursday, September 29, 2011 2:20 AM
    Wednesday, September 28, 2011 3:53 AM
    Moderator
  • Thanks. It works :) 
    Michael Sync: blog: http://michaelsync.net
    Thursday, September 29, 2011 2:21 AM