您好我正在编写linq查询以获取两个表中的列的最大计数。我写了sql查询,它在下面。
select MAX(p.dispalyOrder) from NCT_Process p INNER JOIN NCT_Process_Settings s ON
p.projectId =s.projectId AND p.level=s.level
我试着如下所示。
dbObject = (from c in entityObject.NCT_Process_Settings
join process in entityObject.NCT_Process on c.projectId equals process.projectId
join level in entityObject.NCT_Process on c.level equals level.level
select new settingsobject
{
MAX(p.dispalyOrder) to some propert of settingsobject
}).Tolist();
我不确定如何从流程表中获取最大显示顺序。任何帮助,将不胜感激。谢谢。
答案 0 :(得分:2)
您只需选择该值,然后在查询中调用max = (from c in entityObject.NCT_Process_Settings
join p in entityObject.NCT_Process
on new { c.projectId, c.level } equals new { p.projectId, p.level }
select p.dispalyOrder).Max();
。另外,要连接多个列,您必须使用要匹配的列创建匿名类。
result = (from c in entityObject.NCT_Process_Settings
join p in entityObject.NCT_Process
on new { c.projectId, c.level } equals new { p.projectId, p.level }
group new{c,p} on 1 into grp
select new {
MaxDisplayOrder = grp.Max(x => x.p.dispalyOrder),
AvgOfSomething = grp.Avgerage(x => x.c.Something),
MinOfASum = grp.Min(x => x.p.SomeNumber + x.c.SomeOtherNumber)
}).Single();
如果您还想要聚合其他列,则可以按常量值进行分组。
Single
请注意result = (from c in entityObject.NCT_Process_Settings
join p in entityObject.NCT_Process
on new { c.projectId, c.level } equals new { p.projectId, p.level }
group p.displayOrder on c.Id into grp
select new {
MaxDisplayOrder = grp.Max(),
Id = grp.Key
}).ToList();
的使用,因为常量上的group by只会产生一行。
或者,如果您只想获得按其他列分组的最大显示顺序,请执行此操作
Key
请注意,on
是您在grp
之后放置的内容,on
是ToList
之前的值的集合。在这种情况下,可能会有多个结果,因此您可以使用<Window.Resources>
<Style x:Key="MyLabelStyle" TargetType="Label">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true"
CornerRadius="3">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
来运行查询。