当我像往常一样尝试将linq查询转换为列表时遇到错误。
这是我的查询:
var Services = (from sa in _ctx.ServiceAttrs
join pp in _ctx.ProcessorProducts
on new { ServiceId = sa.ServiceID, PrsnPk = ActivePrsnPk } equals
new { ServiceId = pp.ServiceID, PrsnPk = pp.PrsnPK } into tmp
from PersonServices in tmp.DefaultIfEmpty()
.Select(PersonServices => new ReviewerServiceDto()
{
ServiceId = sa.ServiceID,
ServiceAliasDescription = sa.ServiceAlias,
IsSelected = (PersonServices.IsActivated == null)
? false
: true,
}).OrderBy(dto => dto.ServiceAliasDescription).ToList();
我正在ToList()处重新上线。告诉我可以删除括号,但是当我删除括号时,它将不再唤起转换为列表的方法...
我以为我在某个地方缺少括号,但是对我来说看起来不错。
答案 0 :(得分:1)
首先,您要打开一个支架而不关闭它。
您正在将master
和Option Explicit
Sub Web_Table_Option_One()
Dim html As Object: Set html = CreateObject("htmlfile")
Dim result As String
Dim Clip As Object: Set Clip = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
With CreateObject("MSXML2.XMLHTTP.6.0")
.Open "GET", "https://weather.gc.ca/warnings/index_e.html", False
.send
result = .responseText
End With
If Len(result) > 0 Then html.body.innerhtml = result
Clip.SetText html.getElementsByTagName("table")(0).outerhtml
Clip.PutInClipboard
With ThisWorkbook.Sheets("Sheet2")
.Cells.ClearContents
.Range("A1").Select
.PasteSpecial Format:="Unicode Text"
End With
End Sub
应用于:OrderBy()
,如果您要有意这样做,那么您所要做的就是在此之后添加一个select并关闭方括号)。
您需要添加一个select子句来告诉您查询中需要哪些数据。 msdn article描述了基本的查询操作和结构。
ToList()
答案 1 :(得分:1)
除了缺少括号外,您还在混合使用LINQ语法和扩展方法语法。在from PersonServices in tmp.DefaultIfEmpty() .Select
中,select不能是扩展方法调用。
这应该有效
var Services = (
from sa in _ctx.ServiceAttrs
join pp in _ctx.ProcessorProducts
on new { ServiceId = sa.ServiceID, PrsnPk = ActivePrsnPk }
equals new { ServiceId = pp.ServiceID, PrsnPk = pp.PrsnPK }
into tmp
from PersonServices in tmp.DefaultIfEmpty()
select new ReviewerServiceDto() {
ServiceId = sa.ServiceID,
ServiceAliasDescription = sa.ServiceAlias,
IsSelected = PersonServices.IsActivated != null
}
)
.OrderBy(dto => dto.ServiceAliasDescription)
.ToList();
适当的缩进有助于区分LINQ语法部分和扩展方法部分。
顺便说一句:第二个from
必须在扩展方法语法中表示为SelectMany
。