使用Linq返回元素的id

时间:2011-12-11 13:53:06

标签: c# linq entity-framework

我有一个函数应该返回参数传递的元素的Id,例如:

public static Int32 getIdByName(string name)
{
    var query = from student in DataAccess.getInstance.StudentSet
                where student.FirstName.Equals(name)
                select student;

    return toto;
}

我不知道如何将结果toto转换为Int32并将其返回!!

2 个答案:

答案 0 :(得分:2)

return DataAccess.getInstance.StudentSet.Last(s => s.FirstName == name).Id;

答案 1 :(得分:1)

public static Int32 getIdByName(string name)
{
    var query = from student in DataAccess.getInstance.StudentSet
                where student.FirstName.Equals(name)
                select student.Id;

    return query.First();
}