如何使用LINQ从C#中的数据库中获取两种不同类型的值?

时间:2016-04-30 10:03:21

标签: c# linq

想要获取id和存储的值。 值是浮动的。 我试图对值进行排序,然后根据相应的ID显示它。

public List GetStuCosineSimilarity()
{
    Dictionary data = new Dictionary<int,>();
    List stuId = new List();    

    data = (from s in DB.Students
            select new
            {
                id = s.StudentId,
                cosine = s.cosineSimilarity
            }).ToList();

    return stuId;
}

2 个答案:

答案 0 :(得分:0)

如果我理解你的话,试试这个:

// data is your dictionary with id and value
var data = DB.Students.ToDictionary(s => s.StudentId, s => s.cosuneSimilarity);

然后,如果您需要返回ID列表,您可以这样做:

return data.Keys.ToList();

答案 1 :(得分:0)

public class Student
{
    public int Id { get; set; }
    public double? Cosine { get; set; }
}

public List<student> GetStuCosineSimilarity()
{ 
    List<Student> lst = new List<Student>();

    lst = (from s in DB.Students
           select new Student()
           {
               Id = s.StudentId,
               Cosine = s.cosineSimilarity
           }).ToList();

    lst = lst.OrderBy(k => k.Cosine).ToList(); // Sorting the float value
    return lst;
}