2016-07-06 105 views
0

情況:我得到了3個互相協作的課程。 1:主(圖形用戶界面) 2:比較(比較值) 3:CompareData(繼承列表值)獲得列表中最低項目的最佳方式

我想取兩個值:一個字符串和一個雙,並把它們放在一個名單。當然,最後還會有不止一個列表項目。列表填好後,我想用字符串得到最低的兩倍,並將它們放在一個標籤中。

這裏是我到目前爲止有:

主營:

public class GlobaleDaten //The second List: VglDaten is the one for my situation 
{ 
    public static List<Daten> AlleDaten = new List<Daten>(); 
    public static List<Vgl> VglDaten = new List<Vgl>(); 
} 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    [...Some Code thats not relevant...] 


//addListAb adds values in a ListBox and should also 
//place them into the list VglDaten 

    public void addListAb() 
    { 
     listBox.Items.Add(Abzahlungsdarlehenrechner.zgName + " " + "[Abzahlungsdarlehen]" + " " +Abzahlungsdarlehenrechner.zmErg.ToString("0.00") + "€" + " " + Abzahlungsdarlehenrechner.zgErg.ToString("0.00") + "€"); 

     Vgl comp = new Vgl(); 
     comp.name = Abzahlungsdarlehenrechner.zgName; 
     comp.gErg = Abzahlungsdarlehenrechner.zgErg; 

     GlobaleDaten.VglDaten.Add(comp); 
    } 

//bVergleich should compare these values from the list 
//and show the lowest value in a label 
    public void bVergleich_Click(object sender, RoutedEventArgs e) 
    { 
     if (listBox.Items.Count <= 0) 
     { 
      MessageBox.Show("Bitte erst Einträge hinzufügen."); 
     } 
     else 
     { 
      VglRechner vglr = new VglRechner(); 
      vglr.Compare(); 

      lVergleich.Content = VglRechner.aErg + " " + "€"; 
     } 
    } 

CompareData:

//Only used for storing the values 
public class Vgl : Window 
{ 
    public string name { get; set; } 
    public double gErg { get; set; } 
} 

比較:

public class VglRechner 
{ 

    public static string aName; 
    public static double aErg; 

    public void Compare(Vgl comp) 
    { 

    //I'm not sure if this is the right way to compare the values 
    //correct me if I'm wrong please :) 
     double low = GlobaleDaten.VglDaten.Min(c => comp.gErg); 

     aErg = low; 
     aName = comp.name; 
    } 
} 

回答

1

使用Enumerable.Min是正確的辦法得到最低值,但不會得到屬於該值的string,因此Vgl實例。

你可以使用這種方法:

Vgl lowestItem = GlobaleDaten.VglDaten.OrderBy(c => c.gErg).First(); 
aErg = lowestItem.gErg; 
aName = lowestItem.name; 
+0

的作品比較完美,謝謝:)不過我有一個問題:當我想設置的標籤aErg的價值和aName它仍然是儘管VglRechner.aErg是正確的,不是嗎? –

+0

對我的評論:與你或我的代碼無關。這個標籤有一個錯誤的屬性,抱歉讓你感到困惑:) –