Convert a query to an object using LINQ TO SQL

I am working with LINQ TO SQL in C # to connect to an SQL database

and I have a table in the DataBase called Person that contains information about the faces and has the following fields Person_Id , First_Name , Last_Name , Email , Password

I have the following query that returns a single row if it is matched:

 LINQDataContext data = new LINQDataContext(); var query = from a in data.Persons where a.Email == "Any Email String" select a; 

my question is how to convert query to an instance of the Equivalent class, which defines:

 class Person { public int person_id; public string First_Name; public string Last_Name; public string E_mail; public string Password; public Person() { } } 
+4
source share
2 answers

Like this:

 Person query = (from a in data.Persons where a.Email == "Any Email String" select new Person { person_Id = a.Id, and so on }).FirstOrDefault(); 
+7
source

I will do it something like this:

 var query = (from a in data.Persons where a.Email == "Unique Email String" select new Person { person_Id = a.Id, etc etc }); //By using this code, you can add more conditions on query as well.. //Now the database hit will be made var person = query.FirstOrDefault(); 
+2
source

Source: https://habr.com/ru/post/1491394/


All Articles