Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert query to object using LINQ TO SQL

Tags:

c#

linq-to-sql

I'm Working with LINQ TO SQL in C# to connect to SQL Database

and I have a table on DataBase called Person which holds information about persons and have the following fields Person_Id, First_Name,Last_Name,Email,Password

I have the following query which returns one row if there 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 the query to an instance of Equivalent class which define is :

class Person
{
    public int person_id;
    public string First_Name;
    public string Last_Name;
    public string E_mail;
    public string Password;
    public Person() { }
} 
like image 502
Mohamad Ghanem Avatar asked Jan 12 '23 22:01

Mohamad Ghanem


1 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();
like image 186
gzaxx Avatar answered Jan 21 '23 19:01

gzaxx