I have a class called Students
public class Students
{
private string name ;
private string age;
private string adress;
//Constructor
public Students (string name,string age,string adress)
{
this.name=name;
this.age=age;
this.adress=adress;
}
}
and a Query myQuery
:
Select h.name
b.age,
v.adress
FROM Tb.Students h
INNER JOIN db.tb_1 d
ON h.id= d.id
INNER JOIN db.TB_2 v
ON d.id = v.id
Now when I run this :
Connection conn = new Connection();
OracleConnection connection = conn.GetDBConnection();
var studentsInfo= connection.Query<Students>(myQuery).ToList();
I noticed studentsInfo
has results and row size is loaded but when I put the cursor on studentsInfo
, the properties , Name
, Age
, Adress
are empty
What I am missing ? How to assign the result of the query to my properties so I can use them like that :
foreach ( var student in studentsInfo )
{
console.writeline(sutdent.age);
}
Not sure this will help, but it looks like what you have should work. I tend to work with SQL Server, but the ADO.NET layer is mostly the same, and I tried the following case:
using Dapper;
using System.Data.SqlClient;
using System.Linq;
public class Students
{
private string name;
private string age;
private string adress;
public Students(string name, string age, string adress)
{
this.name = name;
this.age = age;
this.adress = adress;
}
public override string ToString() => $"{name}, {age}, {adress}";
}
static class P
{
static void Main()
{
using (var connection = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=SSPI;"))
{
const string myQuery = "select 'fred' as [name], '27' as [age], 'somewhere' as [adress]";
var studentsInfo = connection.Query<Students>(myQuery).ToList();
foreach(var student in studentsInfo)
{
System.Console.WriteLine(student);
}
}
}
}
It worked fine, outputting:
fred, 27, somewhere
So my main thoughts would be: does the query output what you think it outputs? Is the data what you think it is? Also: are you sure that age
is a string
?