Indhi
Indhi

Reputation: 1694

Display the lastest entry from Detail Table- using Linq to entities

I have a Datagrid as Master Table and a Pie-Chart for Detail's display. I have a list of Servers with their ServerID's in the Master Table like this-

enter image description here

and my Details table is

enter image description here

I would like to display the latest value of RAM(depending on the RAM_ID)- Used & Available in the Pie chart when I click on the respective serverID.

for example the values for server 1 will be displayed as (from Ram_ID) enter image description here

for server 2

enter image description here

& for server 3

enter image description here

What I have Tried,

public override void OnApplyTemplate()
    {
        using (DB_Entities db = new DB_Entities())
        {

          var ramRow = db.UsageRAMs.OrderByDescending(c => c.ID).First();                

          var Ram = new List<UsageRAM>() { new UsageRAM() { name = "Used"  , value = ramRow.Used },
                                          new UsageRAM() { name = "Available" , value = ramRow.Available}};

          this.ramViewSource = (CollectionViewSource)this.FindResource("ramView");
          ramViewSource.Source = Ram;
        }
        base.OnApplyTemplate();
    }

XAML

<customControls:LabeledPieChart x:Name="labeledPieChartRAM" BorderBrush="Transparent" Margin="2,10,35,27" Grid.RowSpan="3" Grid.Column="1">
       <customControls:LabeledPieChart.LegendStyle>
       <Style TargetType="dv:Legend">
        <Setter Property="Width" Value="0" />
       </Style>
     </customControls:LabeledPieChart.LegendStyle>
    <customControls:LabeledPieChart.Series>
   <customControls:LabeledPieSeries x:Name="labeledPieSeriesRAM" 
      ItemsSource="{Binding Source={StaticResource ramView}}"                                                              
      PieChartLabelStyle="{StaticResource pieChartLabelStyle}" 
      PieChartLabelItemTemplate="{StaticResource pieChartLabelDataTemplate}" 
      IndependentValuePath="name" DependentValuePath="value" IsSelectionEnabled="True"
      LabelDisplayMode="ArcMidpoint" />
     </customControls:LabeledPieChart.Series>
    </customControls:LabeledPieChart>

output

enter image description here

The output for the above code displays the latest value from the details table, i.e it takes the values from RAM_ID 8, but I need values for each server when selected. how to achieve this ? kindly help

Upvotes: 1

Views: 188

Answers (2)

YOusaFZai
YOusaFZai

Reputation: 698

Try Something Like this

    //In this query i am grouping the record by ServerID and Selecting ServerID+ Max(Ram_ID )

     using (DB_Entities db = new DB_Entities())
     {
        var lstRamList = (from usgRam in DB.UsageRAMs 
           group usgRam  by new { usgRam.ServerID} 
           into grp
           select
           new
           {
           TMP_ID_ServerID = grp.Key.ServerID,
           TMP_MAX_RAM_ID= grp.Max(x => x.Ram_ID)}); 


 //in this lower list we save all the record from the Db.UsageRAMs where k.ServerID(from above list) == d.ServerID(from Your DB Context) && k.TMP_MAX_RAM_ID == d.Ram_ID
    var lstRAMListFinal = from d in db.UsageRAMs
    where lstRamList.Any(k =>
                        k.TMP_ID_ServerID== d.ServerID  &&
                        k.TMP_MAX_RAM_ID == d.Ram_ID)
   select d;
}

Upvotes: 1

YOusaFZai
YOusaFZai

Reputation: 698

If i am not wrong you can simply use RECORD_DATE Column in Details table!!!enter image description here

Upvotes: 0

Related Questions