如何使用Linq从C#中的vb.net选择哪里?

时间:2014-07-18 15:25:02

标签: c# vb.net linq

我正在将一个旧的vb.net应用程序转换为C#,而我的VB Linq技能在很多层面都缺乏。我在C#linq中需要这个:

  Dim dtx as New MyDataBaseContext(connectionString)
  Dim reports = new List(Of CustomerReport)

  reports = (From rpt In dtx.CustomerReports Join _
                           crs In dtx.Customer_Reports_Schedules On rpt.CRS_Key Equals crs.CRS_Key _
                           Where Not (crs.CRS_Occurrence.ToUpper.Contains("ADHOC") Or crs.CRS_Occurrence.ToUpper.Contains("OBSOLETE")) _
                           Select rpt Where rpt.Description.Contains(searchValue.Trim)).OrderBy(Function(p) p.Description).OrderBy(Function(p) p.CRS_Key).ToList

到目前为止我所转换的是:

 reports = (from rpt in 
                _context.CustomerReports join crs in _context.Customer_Reports_Schedule on 
                rpt.CRS_Key equals crs.CRS_Key where !(crs.CRS_Occurrence.ToUpper().Contains("ADHOC")
                || crs.CRS_Occurrence.ToUpper().Contains("OBSOLETE"))
       //I am stuck here. I have not done a select where clause of this nature before
                select rpt where rpt.Description.Contains(request.Criterion.Trim())
       //I am also not completely sure what Function(p) does
                    .OrderBy(Function(p) p.Description).OrderBy(Function(p) p.CRS_Key)
                        .ToList());

正如你所看到的,linq中有两点我不确定(见评论)。我之前没有看到linq中的这种select where子句(是的,我用Google搜索了),我不知道函数(p)正在尝试做什么(我在说话的时候正在谷歌搜索)

1 个答案:

答案 0 :(得分:1)

我认为你想要这样的东西虽然我不能完全测试它......

var likeString = string.Format("%{0}%", request.Criterion.Trim());

reports = (from rpt in 
                _context.CustomerReports join crs in _context.Customer_Reports_Schedule on 
                rpt.CRS_Key equals crs.CRS_Key where !(crs.CRS_Occurrence.ToUpper().Contains("ADHOC")
                || crs.CRS_Occurrence.ToUpper().Contains("OBSOLETE"))
       // SqlMethods.Like will give you the string.contains that I think this is doing
                select rpt where SqlMethods.Like(rpt.Description, likeString))
       // This becomes a lamda like Beakie & Chris mentioned...
                    .OrderBy(p => p.Description)
                        .ToList());

这会为您的OrderBySqlMethods.Like使用lambda来替换您应该解决问题的两个部分的rpt.Description.Contains