Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • DevDesi 12 posts 42 karma points
    Oct 15, 2014 @ 16:09
    DevDesi
    0

    Reading from Custom tables with PetaPoco Query method doesn't work

    Hello, I am trying to read data from custom tables in Umrbaco DB with public IEnumerable<T1> Query<T1, T2>(string sql, params object[] args); but I don't get any exception and results are null.

    Here is my source code:

    [TableName("FacultyId")]
        [PrimaryKey("FacultyId", autoIncrement = true)]
        public class Faculty
        {
            public int FacultyId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }
    }
    
    [TableName("Students")]
    [PrimaryKey("Id", autoIncrement = true)]
    public class Student
    {
    
        public int Id { get; set; }
    
        public int StudentUmbracoId { get; set; }
    
        [Required]
        public string Name { get; set; }
    
        [EmailAddress]
        [Required]
        public string Email { get; set; }
    
        [Required]
        public int ClassNumber { get; set; }
    
        [Column("FacultyId")]
        public int FacultyId { get; set; }
    
    }
    

    And my method for calling the SQL query is:

    public IEnumerable<Student> GetStudentsByFaculty()
    {
        IEnumerable<Student> result = database.Query<Student, Faculty>("SELECT Students.*, Faculty.* from Students LEFT JOIN Faculty on Students.FacultyId = Faculty.FacultyId");
        return result;
    }
    

    The result is : Results

    I will be thankful if somebody can explain me where I am wrong.

    Thanks, Desi

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Oct 15, 2014 @ 16:30
    Alex Skrypnyk
    0

    Hi Desi,

    Do you have these tables in your db ?

  • DevDesi 12 posts 42 karma points
    Oct 15, 2014 @ 16:35
    DevDesi
    0

    Hi Alex, Yes, I have them and when run this SQL in server Explorer I receive result. Also I received results in backend too with Fetch<>, please see below:

    public IEnumerable<Student> GetStudentsByFaculty()
    {
                    var result = database.Fetch<Student, Faculty>(@"Select Students.*, Faculty.* from Students LEFT OUTER JOIN Faculty on Students.FacultyId = Faculty.FacultyId");
        return result;
    }  
    

    enter image description here

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Oct 19, 2014 @ 22:12
    Andy Butland
    0

    I think in this case you should use Fetch - there's a slight difference in the use of Query in that the latter doesn't load all the data into memory.  See Query vs Fetch here.  Probably in your first example if you expanded the results view you would see the data - but as I say, it looks to me that Fetch is what you would want to use here anyway.

    Hope that helps

    Andy

  • DevDesi 12 posts 42 karma points
    Oct 20, 2014 @ 09:38
    DevDesi
    0

    Hi Andy, Thanks for your answer and advice. I will look at the article for comparison between Fetch and Query.

    Thanks, Desi

Please Sign in or register to post replies

Write your reply to:

Draft