在 where 子句中使用 where 子句的 Linq 查询导致错误
Linq query with where clause within a where clause causes an error
var largeset =
from inv in context.Invoices
join line in context.InvoiceLines on inv.InvoiceId equals line.InvoiceId
into Lines from linejoin in Lines
join track in context.Tracks on linejoin.TrackId equals track.TrackId
into Tracks
select new
{
Invoice = inv,
Line = Lines,
Track = Tracks
};
// Filter by search term
if (!string.IsNullOrEmpty(SearchTerm))
{
largeset = largeset.Where(x =>
x.Invoice.Customer.LastName.StartsWith(SearchTerm) ||
x.Invoice.Customer.FirstName.StartsWith(SearchTerm) ||
x.Track.Where(t => t.Name.Contains(SearchTerm)).Count > 0);
}
这段代码的最后一行
x.Track.Where(t => t.Name.Contains(SearchTerm)).Count > 0
导致错误
Error CS0019 Operator '>' cannot be applied to operands of type
'method group' and 'int'
提前致谢。
https://msdn.microsoft.com/en-us/library/bb338038(v=vs.100).aspx
您需要使用 Count()
调用方法 Count
,而不是像 属性.
那样仅仅引用成员名称
var largeset =
from inv in context.Invoices
join line in context.InvoiceLines on inv.InvoiceId equals line.InvoiceId
into Lines from linejoin in Lines
join track in context.Tracks on linejoin.TrackId equals track.TrackId
into Tracks
select new
{
Invoice = inv,
Line = Lines,
Track = Tracks
};
// Filter by search term
if (!string.IsNullOrEmpty(SearchTerm))
{
largeset = largeset.Where(x =>
x.Invoice.Customer.LastName.StartsWith(SearchTerm) ||
x.Invoice.Customer.FirstName.StartsWith(SearchTerm) ||
x.Track.Where(t => t.Name.Contains(SearchTerm)).Count > 0);
}
这段代码的最后一行
x.Track.Where(t => t.Name.Contains(SearchTerm)).Count > 0
导致错误
Error CS0019 Operator '>' cannot be applied to operands of type 'method group' and 'int'
提前致谢。
https://msdn.microsoft.com/en-us/library/bb338038(v=vs.100).aspx
您需要使用 Count()
调用方法 Count
,而不是像 属性.