LINQ to object -- 需要带有 AND 和 OR 条件的 where 子句
我是 LINQ 的菜鸟,我无法构建具有多个条件的 where 子句。基本上我需要类似的东西:
SELECT *
FROM table
WHERE location = @location AND (unitype = "L" OR unittype = "D")
或者:
SELECT *
FROM table
WHERE location = @location AND unitype IN ("D", "L")
我可以执行 AND 或 OR,但我不知道如何在一个语句中执行这两个操作。我现有的查询如下。它有效,但不受位置限制。我可以成功地按位置和单个单位类型进行限制,但不能同时限制两者。我不确定我是否需要子查询或其他方法——一个正确方向的指针将不胜感激!
gvFixed.ItemsSource = from fs in fixedSystem
//where fs.Owner.Location.ToString() == location && (fs.UnitType == "D" || fs.UnitType == "L")
where fs.UnitType == "D" || fs.UnitType == "L"
group fs by new { fs.Owner.Name } into grouping
select new FixedSystem()
{
Owner = new SystemOwner() { Name = grouping.Key.Name },
OnHand = grouping.Sum(fs => fs.OnHand),
FMC = grouping.Sum(fs => fs.FMC),
NMCS = grouping.Sum(fs => fs.NMCS),
NMCM = grouping.Sum(fs => fs.NMCM),
PMCS = grouping.Sum(fs => fs.PMCS),
PMCM = grouping.Sum(fs => fs.PMCM),
FMCPercent = Math.Round(grouping.Average(fs => fs.FMCPercent)),
ScheduleS = grouping.Sum(fs => fs.ScheduleS),
ScheduleM = grouping.Sum(fs => fs.ScheduleM),
ScheduleT = grouping.Sum(fs => fs.ScheduleT)
};
错误全文(查询结果将进入 Telerik 数据网格):
Unhandled Error in Silverlight Application Code: 4004
Category: ManagedRuntimeError
Message: System.ArgumentOutOfRangeException: Index was out of range.
Must be non- negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Telerik.Windows.Data.KeyedCollection.get_Item(Int32 index)
at Telerik.Windows.Data.QueryableCollectionView.InternalGetItemAt(Int32 index)
at Telerik.Windows.Data.QueryableCollectionView.GetItemAt(Int32 index)
at Telerik.Windows.Data.DataItemCollection.get_Item(Int32 index)
at Telerik.Windows.Controls.GridView.GridViewItemContainerGenerator.Generator.GenerateNext(Boolean stopAtRealized, Boolean& isNewlyRealized)
at Telerik.Windows.Controls.GridView.GridViewItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.GenerateNext(Boolean& isNewlyRealized)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.GenerateNextChild(IItemContainerGenerator generator, Int32 childIndex)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
I am a noob to LINQ and I have not been able to build a where clause that has multiple conditions. Basically I need something like:
SELECT *
FROM table
WHERE location = @location AND (unitype = "L" OR unittype = "D")
Alternately:
SELECT *
FROM table
WHERE location = @location AND unitype IN ("D", "L")
I can do AND or OR, but I can't figure out how to do both in one statement. My existing query is as follows. It works, but doesn't limit by location. I can successfully limit by location and a single unit type, but not both. I'm not sure if I need a subquery or some other method -- a pointer in the right direction would be appreciated!
gvFixed.ItemsSource = from fs in fixedSystem
//where fs.Owner.Location.ToString() == location && (fs.UnitType == "D" || fs.UnitType == "L")
where fs.UnitType == "D" || fs.UnitType == "L"
group fs by new { fs.Owner.Name } into grouping
select new FixedSystem()
{
Owner = new SystemOwner() { Name = grouping.Key.Name },
OnHand = grouping.Sum(fs => fs.OnHand),
FMC = grouping.Sum(fs => fs.FMC),
NMCS = grouping.Sum(fs => fs.NMCS),
NMCM = grouping.Sum(fs => fs.NMCM),
PMCS = grouping.Sum(fs => fs.PMCS),
PMCM = grouping.Sum(fs => fs.PMCM),
FMCPercent = Math.Round(grouping.Average(fs => fs.FMCPercent)),
ScheduleS = grouping.Sum(fs => fs.ScheduleS),
ScheduleM = grouping.Sum(fs => fs.ScheduleM),
ScheduleT = grouping.Sum(fs => fs.ScheduleT)
};
Full text of error (results of the query are going into a Telerik data grid):
Unhandled Error in Silverlight Application Code: 4004
Category: ManagedRuntimeError
Message: System.ArgumentOutOfRangeException: Index was out of range.
Must be non- negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Telerik.Windows.Data.KeyedCollection.get_Item(Int32 index)
at Telerik.Windows.Data.QueryableCollectionView.InternalGetItemAt(Int32 index)
at Telerik.Windows.Data.QueryableCollectionView.GetItemAt(Int32 index)
at Telerik.Windows.Data.DataItemCollection.get_Item(Int32 index)
at Telerik.Windows.Controls.GridView.GridViewItemContainerGenerator.Generator.GenerateNext(Boolean stopAtRealized, Boolean& isNewlyRealized)
at Telerik.Windows.Controls.GridView.GridViewItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.GenerateNext(Boolean& isNewlyRealized)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.GenerateNextChild(IItemContainerGenerator generator, Int32 childIndex)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉这么久才回到他身边;办公室的其他项目受到干扰。解决方案如下:
我必须明确地将固定类型属性放入所选对象中。事后看来这是有道理的,但当我最初编写该语句时,似乎没有必要指定已经定义的属性。
Sorry it took so long to get back to his; other projects at the office interfered. Here's the solution:
I had to explicity put the FixedType property into the selected object. It makes sense in hindsight but it didn't seem necessary to specify an already defined property when I initially wrote the statement.