在VB.NET中数据库到linq [英] Datatable to linq in VB.NET

查看:60
本文介绍了在VB.NET中数据库到linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我改写Linq下面的foreach循环



我尝试过:



Can anyone help me rewriting the below foreach loop in Linq

What I have tried:

Sub Main()
    Dim Id As String
    Const FILE_NAME As String = "File_Name"
    Const FILE_ID As String = "File_Id"
    Id = Nothing
    Dim fileName As String = "CT_TST_123_170102"
    Dim tempFileName As String

    Dim dt As New DataTable
    dt.Columns.Add("File_Id", GetType(Integer))
    dt.Columns.Add("File_Name", GetType(String))
    dt.Rows.Add(1, "MT_FSX_*")
    dt.Rows.Add(2, "KJ_FSX_*")
    dt.Rows.Add(3, "CT_TST_*")

    'Can anyone help me rewriting the below section in linq
    '------------------------------------------------------------------------
    For Each dr As DataRow In dt.Rows
        tempFileName = Convert.ToString(dr(FILE_NAME)).Replace("*", "")
        If (fileName.Contains(tempFileName.Trim())) Then
            Id = Convert.ToString(dr(FILE_ID))
            Exit For
        End If
    Next dr
    '-------------------------------------------------------------------------
End Sub

推荐答案

从这里开始:

编程指南(LINQ to DataSet) [ ^ ]

LINQ to DataSet [ ^ ]

LINQ to DataSet示例 [ ^ ]

查询数据集(LINQ to DataSet) [ ^ ]



注意:
Start here:
Programming Guide (LINQ to DataSet)[^]
LINQ to DataSet[^]
LINQ to DataSet Examples[^]
Querying DataSets (LINQ to DataSet)[^]

Note:

DataTable类没有实现任何一个接口,因此如果要将DataTable用作源,则必须调用 AsEnumerable 方法在LINQ查询的From子句中。

The DataTable class does not implement either interface, so you must call the AsEnumerable method if you want to use the DataTable as a source in the From clause of a LINQ query.





所以,如果你想获得名称不是eq的文件名用空字符串...



So, if you want to get file names where the name is not equal to empty string...

Dim qry = From dr As DataRow in dt.AsEnumerable() _
    Where Not dr.Field(Of String)("FileName").Contains("*")
    Select dr





祝你好运!



Godd luck!


由ReSharper提供。未经测试,我甚至没有尝试编译,但它应该让你超过驼峰...

Courtesy of ReSharper. untested and I haven't even attempted to compile but it should get you over the hump...
Id = (From dr As DataRow In dt.Rows
        Let tempFileName = Convert.ToString(dr(FILE_NAME)).Replace("*", "")
        Where (fileName.Contains(tempFileName.Trim()))
        Select Convert.ToString(dr(FILE_ID))).FirstOrDefault()


这篇关于在VB.NET中数据库到linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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