获取WPF Listview行的值,并使用Linq查询将它们与数据库字段进行比较

时间:2016-04-29 10:02:13

标签: c# wpf linq

我想将我的数据库中的ECH_INTITULE与listview行的值进行比较但我的问题是我无法访问Linq查询中的Listview项

这是我的Listview的Xaml代码:

<ListView x:Name="listView" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding}" DataContext="query" HorizontalAlignment="Left" Height="430" Margin="24,47,0,0" VerticalAlignment="Top" Width="940" >
        <ListView.View>
            <GridView x:Name="gridView">
                <GridViewColumn Header="Fournisseur" DisplayMemberBinding="{Binding fournisseur}" Width="280"/>
                <GridViewColumn Header ="Adresse Mail" DisplayMemberBinding="{Binding email}" Width="270"/>
                <GridViewColumn Header ="Date" DisplayMemberBinding="{Binding date}" Width="150"/>
                <GridViewColumn Header="Fichier CSV" DisplayMemberBinding="{Binding xxx}" Width="105"/>
                <GridViewColumn Header="Fichier PDF" DisplayMemberBinding="{Binding xxx}" Width="105"/>
            </GridView>
        </ListView.View>
    </ListView>

这是我的c#代码:

//Generate CSV Files for each item in the Listview
        CsvFileDescription outpCsvFileDescription = new CsvFileDescription
        {
            SeparatorChar = ',',
            FirstLineHasColumnNames = true
        };

        for (int i = 0; i < listView.Items.Count; i++)
        {
            var infoEcheances = from f in db.F_ECHEANCES
                                //where f.ECH_Intitule == ???
                                select new { f.ECH_Intitule, f.ECH_DateEch, f.CG_Num, f.ECH_Piece, f.ECH_RefPiece, f.ECH_Montant, f.ECH_Libelle };


            CsvContext cc = new CsvContext();
            string myPath = @"C:\Users\DefaultAccount\Desktop\Projet Top Of Travel\FichiersCSV\";
            string filename = string.Format("Facture{0}.csv", i);
            string finalPath = System.IO.Path.Combine(myPath, filename);
            cc.Write(infoEcheances, finalPath, outpCsvFileDescription);
        }

1 个答案:

答案 0 :(得分:0)

你无法直接比较那样的行。 请尝试使用以下代码。

//Generate CSV Files for each item in the Listview
    CsvFileDescription outpCsvFileDescription = new CsvFileDescription
    {
        SeparatorChar = ',',
        FirstLineHasColumnNames = true
    };

    for (int i = 0; i < listView.Items.Count; i++)
    {
        var infoEcheances = from f in db.F_ECHEANCES
                            where f.ECH_Intitule == listView.Items[i].f.ECH_Intitule
                            select new { f.ECH_Intitule, f.ECH_DateEch, f.CG_Num, f.ECH_Piece, f.ECH_RefPiece, f.ECH_Montant, f.ECH_Libelle };


        CsvContext cc = new CsvContext();
        string myPath = @"C:\Users\DefaultAccount\Desktop\Projet Top Of Travel\FichiersCSV\";
        string filename = string.Format("Facture{0}.csv", i);
        string finalPath = System.IO.Path.Combine(myPath, filename);
        cc.Write(infoEcheances, finalPath, outpCsvFileDescription);
    }