如何使用LINQ To SQL从DataBase中选择不同的数据并具有良好的性能? [英] How to select distinct data from DataBase with LINQ To SQL and have a good performance ?

查看:59
本文介绍了如何使用LINQ To SQL从DataBase中选择不同的数据并具有良好的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想通过LINQ TO SQL FIle从数据库中获取数据,



在我的学生表中,有三列:学校,部门和姓名。

我想通过我所知的学生学校名称选择不同的学生部门。



我的代码是:

Hi Guys,

I want to get the data from database by LINQ TO SQL FIle,

In my Student Table, there are three columns :school, department and Name.
I want to select the distinct student's department through the student's school's name that I have known.

My code is :

DataClassesDataContext d = new DataClassesDataContext();

        IQueryable rets = (from p in d.Students
                                              where p.School=="Computer"
                                    select p).Distinct();





但它不能很好地工作,它会返回一堆不同的数据!

我该怎么办?



But it does not work well ,it will return a heap of data which is not distinct!
What should I do?

推荐答案

如何关于选择p.department而不仅仅选择p。



select p将为你提供所有p.school,p.department或p.name不同的p - 所以在同一所学校/部门中有两个不同名字的学生是不同的 - 如果你只是想要不同的部门选择p.department,它只会给你部门。
How about select p.department rather than just select p.

select p will give you all p's where any of p.school, p.department or p.name differ - so 2 students with different names in the same school / department are distinct - if you just want distinct departments select p.department which will just give you the department.


你好发现,< br / >


尝试使用以下代码更改您的代码:



Hi it-discovery,

Try to change your code with this one :

IQueryable rets = (from p in d.Students
                   where p.School == "Computer"
                   select p.department).Distinct();







我希望这个帮助,

:)




I hope this help,
:)


这篇关于如何使用LINQ To SQL从DataBase中选择不同的数据并具有良好的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆