如何使用LINQ在DataTable中找到Last项 [英] How do I find the Last item in a DataTable using LINQ

查看:103
本文介绍了如何使用LINQ在DataTable中找到Last项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个只包含两个数据库的整个数据库读入一个DataSet。



只使用DataSet中的一个表,在本例中为tblPCNames。我想编写一个LINQ查询,它将返回PCID字段中的最后一个ID号。



现在,这是在MS-Access 2010数据库的SQL中完成的:

  SELECT  Last(tblPCNames.PCID) AS  LastOfPCID 
FROM tblPCNames;



这是否可以使用内存中的DataSet(DataTables)或者我必须查询数据源(数据库)直接。



我在互联网上搜索了Visual Studio 2008示例,但一直无法找到它们。一个站点说LINQ不支持像Last这样的Element调用。如果它们是正确的,它是否有点挫败了在DataSet中存储数据和使用LINQ的目的?



谢谢,

MRM256

解决方案

最后 [ ^ ]并返回序列的最后一个元素。

第一个和最后一个功能 [ ^ ] repsectively从第一个或最后一个记录返回字段值查询返回的结果集



但我不确定你想要实现这一点。可能你想得到 MAX(PCID)的价值。请阅读我对该问题的评论。


我在互联网上找到了大部分此类代码。我把它调整到这个小函数。



 公共 函数 GetNextPriKeyID( ByVal  dt  As  DataTable) As   Int32  
' 目的:用于手动生成的主键。
' 主要位置key是表格中的第一个
列。
' 参数:dt As DataTable。
' 返回:新主键ID

' 定义&返回表格中的最大主键
Dim maxNum As Int32 =(从n dt _
选择 n.Field( Int32 )( 0 ))。Max()
' Debug.Print(主键中的最大数量& maxNum&。)
' 在maxNum中添加一个以创建新的唯一ID
maxNum + = 1
' 返回新的主键
返回 maxNum
结束 功能


I read an entire database consisting of only two into a DataSet.

Using just one table from the DataSet, in this case tblPCNames. I want to write a LINQ query that will return the last ID number in the PCID field.

This is now it is done in SQL on an MS-Access 2010 database:

SELECT Last(tblPCNames.PCID) AS LastOfPCID
FROM tblPCNames;


Is this possible using the DataSet(DataTables) in memory or will I have to query the data source(database) directly.

I have searched the Internet for Visual Studio 2008 examples, but have not been able to find them. One site said "Element" calls like "Last" are not supported in LINQ. If they are correct doesn't it kinda defeat the purpose of storing data inside a DataSet and using LINQ?

Thanks,
MRM256

解决方案

Last[^] is supported by Linq query and returns the last element of a sequence.
First and Last Functions[^] repsectively return field value from the first or last record in the result set returned by a query.

But i'm not sure you want to achieve that. Probably you want to get MAX(PCID) value. Please, read my comment to the question.


I found most of this code on the Internet. I tweaked it to this little function.

Public Function GetNextPriKeyID(ByVal dt As DataTable) As Int32
        'Purpose:       For manually generated primary keys.
        '               Where the primary key is the first
        '               column in the table. 
        'Parameters:    dt As DataTable. 
        'Returns:       New Primary Key ID

        'Define & return maximum Primary key in table
        Dim maxNum As Int32 = (From n In dt _
                Select n.Field(Of Int32)(0)).Max()
        'Debug.Print("The max number in Primary Key " & maxNum & ".")
        'Add one to maxNum to make new unique ID
        maxNum += 1
        'Return the new Primary Key
        Return maxNum
    End Function


这篇关于如何使用LINQ在DataTable中找到Last项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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