4

我正在使用 C# 中的 LINQ TO SQL 连接到 SQL 数据库

我在数据库上有一个名为的表,其中Person包含有关人员的信息,并具有以下Person_Id字段,,,,,First_NameLast_NameEmailPassword

我有以下查询,如果匹配则返回一行:

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

我的问题是如何将queryEquivalent 类的实例转换为定义为:

class Person
{
    public int person_id;
    public string First_Name;
    public string Last_Name;
    public string E_mail;
    public string Password;
    public Person() { }
} 
4

2 回答 2

7

像这样:

Person query = (from a in data.Persons
            where a.Email == "Any Email String"
            select new Person { person_Id = a.Id, and so on }).FirstOrDefault();
于 2013-07-15T08:53:17.043 回答
2

我会这样做:

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();
于 2013-07-15T08:58:20.980 回答