如何从linq中获取100条记录基于条件查询 [英] how to take 100 records from linq query based on a condition

查看:344
本文介绍了如何从linq中获取100条记录基于条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,这将给结果集。根据条件我要带100条记录。这意味着。我有一个变量x,如果x的值是100,然后我必须做的。取(100)一样,我需要得到完整的记录。

  VAR ABC =(从ST中Context.STopics 
,其中st.IsActive ==真&放大器;&安培; st.StudentID == 123
选择新的结果()
{
NAME = st.name})了ToList()取(100)。


解决方案

其中一个最重要的概念在 SQL TOP 命令按。你不应该使用 TOP 没有 ORDER BY ,因为它可能会返回在不同的情况不同的结果。



同样的概念也适用于LINQ了。

  VAR的结果= Context.STopics.Where (ST = GT; st.IsActive和放大器;&安培; st.StudentID == 123)
。选择(ST = GT;新的结果(){名称= st.name})
.OrderBy(R = GT; r.name)
。取(100).ToList();

执行和跳过操作以及只定义了针对有序集合。的<一个HREF =https://msdn.microsoft.com/en-us/library/bb386988(v=vs.110).aspx相对=nofollow>更多


资讯

I have a query, which will give the result set . based on a condition I want to take the 100 records. that means . I have a variable x, if the value of x is 100 then I have to do .take(100) else I need to get the complete records.

var abc=(from st in Context.STopics 
where  st.IsActive==true && st.StudentID == 123 
select new result()
{
 name = st.name }).ToList().Take(100);

解决方案

One of the most important concept in SQL TOP command is order by. You should not use TOP without order by because it may return different results at different situations.

The same concept is applicable to linq too.

 var results = Context.STopics.Where(st => st.IsActive && st.StudentID == 123)
              .Select(st => new result(){name = st.name})
              .OrderBy(r => r.name)                 
              .Take(100).ToList();

Take and Skip operations are well defined only against ordered sets. More info

这篇关于如何从linq中获取100条记录基于条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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