VB.Net中如何使用Lambda表达式进行查询操作?

2026-05-06 11:431阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计207个文字,预计阅读时间需要1分钟。

VB.Net中如何使用Lambda表达式进行查询操作?

我知道我可以将这样的查询从以下形式改写为Lambda表达式:

原始查询:sqlDim myList As List(Of Tickets)=(From a In db.myTable Where a.ID=id AndAlso a.IsOnline=True).ToList

Lambda表达式:csharpDim myList As List(Of Tickets)=db.Where(Function(x) x.ID=id AndAlso x.IsOnline=True).ToList()

我知道我可以改变这样的查询

VB.Net中如何使用Lambda表达式进行查询操作?

Dim myList As List(Of Tickets) = (From a In db.myTable Where a.ID = id AndAlso a.IsOnline = True).ToList

到lambda

Dim myList as List(of Tickets) = db.Where(Function(x) x.ID = id ANDAlso a.IsOnline = true).ToList

我如何编写lambda版本并告诉它用结果创建一个新对象?像这样:

Dim myList As List(Of Tickets) = (From a In db.myTable Where a.ID = id AndAlso a.IsOnline = True Select New TicketType With { .Id = a.rsID.ToString, .Title = a.rsTitle}).ToList 您正在寻找Select()方法:

.Select(Function(x) New TicketType With { ... })

本文共计207个文字,预计阅读时间需要1分钟。

VB.Net中如何使用Lambda表达式进行查询操作?

我知道我可以将这样的查询从以下形式改写为Lambda表达式:

原始查询:sqlDim myList As List(Of Tickets)=(From a In db.myTable Where a.ID=id AndAlso a.IsOnline=True).ToList

Lambda表达式:csharpDim myList As List(Of Tickets)=db.Where(Function(x) x.ID=id AndAlso x.IsOnline=True).ToList()

我知道我可以改变这样的查询

VB.Net中如何使用Lambda表达式进行查询操作?

Dim myList As List(Of Tickets) = (From a In db.myTable Where a.ID = id AndAlso a.IsOnline = True).ToList

到lambda

Dim myList as List(of Tickets) = db.Where(Function(x) x.ID = id ANDAlso a.IsOnline = true).ToList

我如何编写lambda版本并告诉它用结果创建一个新对象?像这样:

Dim myList As List(Of Tickets) = (From a In db.myTable Where a.ID = id AndAlso a.IsOnline = True Select New TicketType With { .Id = a.rsID.ToString, .Title = a.rsTitle}).ToList 您正在寻找Select()方法:

.Select(Function(x) New TicketType With { ... })