LINQ to objects - 需要带AND和OR条件的where子句

时间:2011-10-20 14:14:07

标签: linq linq-to-objects

我是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)

1 个答案:

答案 0 :(得分:0)

对不起,花了这么长时间才回到他身边;办公室的其他项目受到干扰。这是解决方案:

gvFixed.ItemsSource = from fs in fixedSystem
where (fs.UnitType == "D" || fs.UnitType == "L") && fs.Owner.Location.ToString() == item.Name
group fs by fs.FixedType into grouping
select new FixedSystem()
{
    Owner = new SystemOwner(),
    FixedType = grouping.Key,
    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)
};

我不得不明确地将FixedType属性放入选定的对象中。事后才有意义,但在我最初编写语句时,似乎没有必要指定一个已定义的属性。