vb.net只从linq.IQueryable返回选定的列/字段(out out t)

时间:2015-05-14 01:35:53

标签: vb.net linq

抱歉我的英语。

我一直在网上搜索“动态Linq查询”,我找到一个对我有用的。 Guide to Creating Dynamic LINQ Queries。我在在线转换器上将其转换为VB。结果是

  Public Shared Function GetFilteredDataWithPage(Customer As String, Itemdesc As String, JoNumber As String)
    Dim DB As New JoDataClassesDataContext
    Dim DataQuery As IQueryable(Of JoMainTbl) = DB.JoMainTbls

    If Customer.Trim().Length > 0 Then
        DataQuery = DataQuery.Where(Function(a) a.Customer.StartsWith(Customer))
    End If
    If Itemdesc.Trim().Length > 0 Then
        DataQuery = DataQuery.Where(Function(a) a.ItemDescription.StartsWith(Itemdesc))
    End If
    If JobNumber.Trim().Length > 0 Then
        DataQuery = DataQuery.Where(Function(a) a.JoNumber = JoNumber)
    End If
    Return DataQuery
  End Function

我有一个包含5个字段的表

“JONumber,Sample,DateCreated,Customer,ItemDesc”

并显示它

DataGridView.DataSource = GetFilteredData(TxtCustomer.Text, TxtItemDesk.Text, TxtJobNumber.Text)

问题是我希望它只按此顺序显示文件“JONumber,Customer,ItemDesc”。非常感谢所有的帮助。提前谢谢你。

1 个答案:

答案 0 :(得分:0)

正如@MarcinJuraszek在评论中建议的那样,你应该使用这样的Select

  Public Shared Function GetFilteredDataWithPage(Customer As String, Itemdesc As String, JoNumber As String) 
    Dim DB As New JoDataClassesDataContext
    Dim DataQuery As IQueryable(Of JoMainTbl) = DB.JoMainTbls

    If Customer.Trim().Length > 0 Then
        DataQuery = DataQuery.Where(Function(a) a.Customer.StartsWith(Customer))
    End If
    If Itemdesc.Trim().Length > 0 Then
        DataQuery = DataQuery.Where(Function(a) a.ItemDescription.StartsWith(Itemdesc))
    End If
    If JobNumber.Trim().Length > 0 Then
        DataQuery = DataQuery.Where(Function(a) a.JoNumber = JoNumber)
    End If
    Return DataQuery.Select(Function(x) New With {.JONumber=x.JONumber, .Customer=x.Customer, .ItemDesc=x.ItemDesc})
  End Function