locked
Select one field with linq entity framework RRS feed

  • Question

  • User-1316533924 posted

    Hi,

    I trying to do something like this:

    Select quantidade from AlimentoRefeicaos

    And I have a object

     public class AlimentoRefeicao
        {
            public int Id { get; set; }
            public virtual Alimento Alimento { get; set; }
            public virtual Refeicao Refeicao { get; set; }
            public double Quantidade { get; set; }
    
        }

    And in my service implementation i have a method:

      public List<AlimentoRefeicao> findAllQtd()
            {
                using (NutricaoContext mde = new NutricaoContext())
                {
                    return mde.AlimentoRefeicao.Select(column => new AlimentoRefeicao
                        {Quantidade = column.Quantidade}).ToList();
                };
            }

    when I invoke this method I get an exception

    An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code Additional information: The entity or complex type 'ControleNutricionalService.Models.AlimentoRefeicao' cannot be constructed in a LINQ to Entities query.

    Tuesday, June 16, 2015 2:46 AM

Answers

  • User1970566204 posted

    Hi,

    a) we can solve this one diffrent ways ....like suing  "DTO class"  or  "Anonymous"  or  "select Anonymous + inject into entity class"

    1)  herewith  example for "select Anonymous + inject into entity class"

    public List<AlimentoRefeicao> findAllQtd()
            {
                using (NutricaoContext mde = new NutricaoContext())
                {
                    var anyms_AlimentoRefeicao= mde.AlimentoRefeicao.Select(
                                 column => new 
                                           {Quantidade = column.Quantidade}).ToList();
    
                         
                  way 1 : 
    return anyms_AlimentoRefeicao.Select( column => new AlimentoRefeicao {Quantidade = column.Quantidade}).ToList(); or way 2 :
    List<AlimentoRefeicao> result = new List<AlimentoRefeicao>(); foreach(var currentAlimentoRefeicao in anyms_AlimentoRefeicao) { result.add(new AlimentoRefeicao{ Quantidade = currentAlimentoRefeicao.Quantidade }); } return result ;
    }; }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 16, 2015 4:31 AM

All replies

  • User1970566204 posted

    Hi,

    a) we can solve this one diffrent ways ....like suing  "DTO class"  or  "Anonymous"  or  "select Anonymous + inject into entity class"

    1)  herewith  example for "select Anonymous + inject into entity class"

    public List<AlimentoRefeicao> findAllQtd()
            {
                using (NutricaoContext mde = new NutricaoContext())
                {
                    var anyms_AlimentoRefeicao= mde.AlimentoRefeicao.Select(
                                 column => new 
                                           {Quantidade = column.Quantidade}).ToList();
    
                         
                  way 1 : 
    return anyms_AlimentoRefeicao.Select( column => new AlimentoRefeicao {Quantidade = column.Quantidade}).ToList(); or way 2 :
    List<AlimentoRefeicao> result = new List<AlimentoRefeicao>(); foreach(var currentAlimentoRefeicao in anyms_AlimentoRefeicao) { result.add(new AlimentoRefeicao{ Quantidade = currentAlimentoRefeicao.Quantidade }); } return result ;
    }; }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 16, 2015 4:31 AM
  • User-1316533924 posted

    asyed4u

    Thank you very much.
    It's worked very well.

    Tuesday, June 16, 2015 7:27 AM
  • User-1316533924 posted

    Hi asyed4u

    You can tell me if I can rename|alias the object Quantidade

    Something like this:

    public List<AlimentoRefeicao> findAllQtd()
            {
                using (NutricaoContext mde = new NutricaoContext())
                {
                    var anyms_AlimentoRefeicao= mde.AlimentoRefeicao.Select(
                                 column => new 
                                           {Quantidade = column.Quantidade}).ToList();
                    return anyms_AlimentoRefeicao.Select(
                                 column => new AlimentoRefeicao 
                                           {Quantidade AS Testing = column.Quantidade}).ToList();
               };
            }

    Thank you

    Wednesday, June 17, 2015 1:14 AM
  • User1970566204 posted

    Hi,

     

    rename|alias the object Quantidade
     .. for display purpose.. ie like Header ?

    use... System.ComponentModel.DataAnnotations. for reference
    
        way 1 : if you class not auto-genereated then..
        public class AlimentoRefeicao
        {
            public int Id { get; set; }
            public virtual Alimento Alimento { get; set; }
            public virtual Refeicao Refeicao { get; set; }
            [Display("Testing")] 
            public double Quantidade { get; set; }
    
        }
    *************************
    
      way 2 : if you class auto-genereated then...
    
     public class AlimentoRefeicao
        {
            public int Id { get; set; }
            public virtual Alimento Alimento { get; set; }
            public virtual Refeicao Refeicao { get; set; }       
            public double Quantidade { get; set; }
        }
    
    [MetadataType(typeof(AlimentoRefeicaoMetadata))]
    public class AlimentoRefeicao 
    {  
        
    }
    
    public class AlimentoRefeicaoMetadata
    {  
            public int Id { get; set; }
            public virtual Alimento Alimento { get; set; }
            public virtual Refeicao Refeicao { get; set; }  
            [Display("Testing")] 
            public double Quantidade { get; set; }
    }
    
    



    Wednesday, June 17, 2015 5:34 AM