Share via

SQL select column belonging to an entity

first100 81 Reputation points
2021-04-29T07:21:50.46+00:00

Hello,

i retrive a from sql query several column, some of them are correctly binded to my object entity, but for more complex object inside i cant have the right bind.

for example if i have :

public class MyBox
  {
        public Box Box { get; set; }
        public bool New { get; set; }
 }

public Box 
{
        public string Name { get; set; }
        public string Color { get; set; }
        public string Desc { get; set; }
} 

var myboxes = executereader("sp_boxes",CommandType.StoredProcedure, SqlParam)

I try to read from stored procedure the records :

  SELECT
     Box.name ,
     Box.color,
     Box.desc,
     State.new,
  FROM tblBoxes as Box,tblState as State

the reading is successful and I have correctly bind the simple property stringNew, but i have null in more complex box object.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,611 Reputation points
    2021-04-30T09:56:01.313+00:00

    How do you store data in an instance of MyBox?

    Is it acceptable to put the obtained data in the datatable and then manually convert it to MyBox?

            public static List<MyBox> GetMyClasses(DataTable dataTable)  
            {  
                List<MyBox> items = dataTable.AsEnumerable().Select(row =>  
                    new MyBox  
                    {  
                        New = row.Field<bool>("New"),  
                        Box = new Box() { Name = row.Field<string>("Name"), Color = row.Field<string>("Color"),Desc = row.Field<string>("Desc") }  
                    }).ToList();  
                return items;  
            }  
    

    Or are you using an orm framework like EF?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. Viorel 127K Reputation points
    2021-04-29T07:30:15.533+00:00

    Try another class:

    public class MyBox : Box
    {
       public bool New { get; set; }
    }
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.