LINQ语句返回元素ROWNUMBER id为==什么? [英] LINQ statement that returns rownumber of element with id == something?
问题描述
如何编写与ID返回元素ROWNUMBER LINQ声明==什么?
有没有直接的方法来做到这一点,我是知道的。你不得不拉动整个查询到客户端,并从那里,你可以在行号项目。作为替代方案,你可以写一个使用ROW_NUMBER一个存储过程,然后打了PROC从LINQ到SQL。
在你的情况,只有这样,你会能够做到这一点。将客户端。请记住下面的语句是不会在服务器上要做到这一点,但会拉低你的整个表,并在客户端...
<$ P $得到指数p>
使用(VAR DC =新DataClasses1DataContext())
{
VAR的结果= dc.Users
.AsEnumerable()//从数据库中选择所有用户和把他们带回给客户端
。选择((用户,指数)=>新建项目//在索引中
{
user.Username,
指数
})
。凡(用户=> user.Username ==sivey); ,项目:为您的特定记录
的foreach(在结果VAR项)
{
Console.WriteLine(的String.Format({1} {0}//过滤器的.index,item.Username));
}
}
How to write LINQ statement that returns ROWNUMBER of element with id == something?
There is no direct way to do this that I'm aware of. You'd have to pull the whole query down to the client, and the from there you could project in the row numbers. As an alternative, you could write a stored procedure that uses ROW_NUMBER, and then hit that proc from Linq to SQL.
In your case, the only way you're going to be able to do this would be client side. Keep in mind that the following statement is NOT going to do this at the server, but will pull down your whole table and get the index at the client...
using (var dc = new DataClasses1DataContext())
{
var result = dc.Users
.AsEnumerable() // select all users from the database and bring them back to the client
.Select((user, index) => new // project in the index
{
user.Username,
index
})
.Where(user => user.Username == "sivey"); // filter for your specific record
foreach (var item in result)
{
Console.WriteLine(string.Format("{0}:{1}", item.index, item.Username));
}
}
这篇关于LINQ语句返回元素ROWNUMBER id为==什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!