如何使用linq查询在C#中从数据库中获取两个不同的值? [英] How do I get two different values from database in C# using linq query?
本文介绍了如何使用linq查询在C#中从数据库中获取两个不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
想要获取id和存储的值。
值是浮点数。
im尝试对值进行排序,然后根据相应的id显示它。
我尝试了什么:
want to get id and a value stored against it.
value is in float.
i m trying to sort the value and then show it according to the corresponding id.
What I have tried:
public List<data> GetStuCosineSimilarity()
{
Dictionary data = new Dictionary<int,double>();
List<data> stuId = new List<data>();
data = (from s in DB.Students
select new
{
id = s.StudentId,
cosine = s.cosineSimilarity
}).ToList();
return stuId;
}
推荐答案
试试这个,
创建一个具有属性的类
Try this,
Create a class with the properties as
public class Student
{
public int Id { get; set; }
public float Cosine { get; set; }
}
你的方法应编辑为
and your method should be edited as
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;
}
这篇关于如何使用linq查询在C#中从数据库中获取两个不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文