2

i have a t_my_class table structure like below (MySql table)

id     class     group     age     name     surname
1      9         A         18      sarah    brown
2      10        B         20      joe      sanders
3      8         A         17      elisa    connor
4      10        C         23      sandra   brown

and i have a struct and a list of that struct

struct MyClass
{
   int id;
   string class;
   string group;
   int age;
   string name;
   string surname;
}
List<MyClass> Students = new List<MyClass>();

Now, can u tell me which LINQ query to use to select all data from t_my_class table to Students List.

3 Answers 3

5

Firstly, that should almost certainly not be a struct - it should be a class. Now, you have a couple of choices; if you do already have a LINQ-enabled ORM hooked up, then it should be simply:

var students = myContext.Students.ToList();

If you aren't already using an ORM tool then a micro-ORM might help, for example dapper-dot-net works with MySql AFAIK, allowing:

var students = connection.Query<Student>("select * from t_my_class").ToList();

With:

class Student
{
   public int Id {get;set;}
   public string Class {get;set;}
   public string Group {get;set;}
   public int Age {get;set;}
   public string Name {get;set;}
   public string Surname {get;set;}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please elaborate on why we shouldnt use a Struct? (purely interested).
@christofr structs are very rare, and unless you know exactly why you are using a struct, a class should be assumed. In particular, structs make poor choices for "object"-type data (and a student is an object, rather than a value). Specific reasons: it is over-sized for a struct, would probably be incorrectly mutable, and the copy-semantics usually cause huge headaches.
1
var students = from std in Students
               select std.

enter image description here

More : Learn SQL to LINQ (Visual Representation)

Comments

0
var students = from p in entities.t_my_class 
select p;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.