[英]Where clause in LINQ query doesn't evaluate against data
I'm populating an ObservableCollection
with the following statement: 我用以下语句填充ObservableCollection
:
foreach (var customer in
collListItem.Select(
item =>
new
{
ResourceName = item["Resource_x0020_Name"].ToString(),
Quantity = (item["Quantity_x0020_Ordered"] ?? 0).ToString(),
CustomerName = item["Title"].ToString()
})
.Select(r => new Customer { ResourceName = r.ResourceName, Quantity = int.Parse(r.Quantity) })
.Where(r => r.CustomerName == "test"))
{
resources.Add(customer);
}
When I remove the .Where()
portion of my LINQ query the statement will fetch all customers. 当我删除LINQ查询的.Where()
部分时,该语句将获取所有客户。 I would like to restrict this to a customer I specify. 我想将其限制为我指定的客户。 Whenever I specify a customer in the where
clause it doesn't appear to evaluate properly. 每当我在where
子句中指定客户时where
它似乎就无法正确评估。 I'm sure my problem is syntactical but after referring to the LINQ documentation I'm a bit unsure where I'm going wrong. 我确定我的问题是句法上的,但是在参考LINQ文档之后,我有点不确定我要去哪里。
You have two calls to Select
. 您有两个Select
呼叫。 The first one projects the original collection to a collection of dynamic objects with three properties ( ResourceName
, Quantity
, and CustomerName
). 第一个将原始集合投影到具有三个属性( ResourceName
, Quantity
和CustomerName
)的动态对象的集合。 Then your second call to Select
projects that collection into another collection of Customer
instances, but only two properties of that class are set ( ResourceName
and Quantity
). 然后,您第二次调用Select
将该项目集合到另一个Customer
实例集合中,但是只设置了该类的两个属性( ResourceName
和Quantity
)。 You are not setting CustomerName
in the second select therefore it is uninitialized when Where
is called. 您没有在第二个选择中设置CustomerName
,因此Where
调用Where
时未初始化它。
I think you really just want one call to Select
, which combines the two calls and skips creating of the dynamic objects. 我认为您真的只希望一次调用Select
,它将两个调用结合起来并跳过动态对象的创建。
For example: 例如:
foreach (var customer in
collListItem.Select(
item =>
new Customer {
ResourceName = item["Resource_x0020_Name"].ToString(),
Quantity = (item["Quantity_x0020_Ordered"] ?? 0),
CustomerName = item["Title"].ToString()
}
)
.Where(r => r.CustomerName == "test"))
{
resources.Add(customer);
}
I'm assuming that item["Quantity_x0020_Ordered"]
is already an integer since you have it combined with an integer via the ??
我假设item["Quantity_x0020_Ordered"]
已经是一个整数,因为您已经通过??
将它与一个整数结合了 operator. 运营商。 Therefore there's no need for the ToString
call from the first select or the int.Parse
call from the second. 因此,不需要从第一个选择调用ToString
或从第二个选择调用int.Parse
。
Just switch the last Select
with the Where
只需切换的最后Select
用Where
foreach (var customer in
collListItem.Select(
item =>
new
{
ResourceName = item["Resource_x0020_Name"].ToString(),
Quantity = (item["Quantity_x0020_Ordered"] ?? 0).ToString(),
CustomerName = item["Title"].ToString()
})
.Where(r => r.CustomerName == "test")
.Select(r => new Customer { ResourceName = r.ResourceName, Quantity = int.Parse(r.Quantity) }))
{
resources.Add(customer);
}
or collapse it like this 或像这样折叠
resources.AddRange(collListItem
.Where(item => item["Title"].ToString() == "test")
.Select(item =>
new Customer {
ResourceName = item["Resource_x0020_Name"].ToString(),
Quantity = item["Quantity_x0020_Ordered"] ?? 0
}
)
);
You always want to query first ( Where
before Select
) because Select
actually creates an entirely new array each time, and it can be expensive to do so, so you want to make sure you do that with the least amount of items as possible, by using all the queries first, so that you have all the items you need first then you can create the array. 你总是想先查询( Where
之前Select
),因为Select
实际上创建了一个全新的阵列中的每个时刻,它可以是昂贵的这样做,所以你要确保你做的项目尽可能最少的,由首先使用所有查询,以便首先拥有所有需要的项目,然后才能创建数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.