简体   繁体   English

如何将DataGrid数据保存到CSV文件而不重新枚举查询结果?

[英]How to save DataGrid data to CSV file without enumerating the query result again?

I am using this command to get data from my query: 我正在使用以下命令从查询中获取数据:

private void loadData()
    {
        numRows = 0;

        try
        {
            data = gpso.gpEntitiesObject.ExecuteStoreQuery<My_Class>(sql);

            dataGrid.ItemsSource = data;
            dataGrid.IsReadOnly = true;
            numRows = dataGrid.Items.Count;
            numRowsTB.Text = "Number of rows: " + numRows;

        }
        catch (Exception ex)
        {
            statusBarTB.Text = "exception: " + ex.Message;
        }
    }

Although this works and I see the data in my DataGrid, I am not able to save the data to a CSV file since I am trying to reenumerate the query result again, which is not allowed. 尽管这可行并且我在DataGrid中看到了数据,但是由于试图重新枚举查询结果,所以无法将数据保存到CSV文件中,这是不允许的。

What would be the right way to get the data and save it to CSV? 什么是获取数据并将其保存到CSV的正确方法? Should I use the DataGrid API to scroll trough the rows and get the data in a foreach() loop? 是否应该使用DataGrid API滚动行并在foreach()循环中获取数据?

Here is my CSV function: 这是我的CSV函数:

    public static string exportToCSV(IEnumerable<My_Class> data, string separator)
    {
        string ishod = "";
        string csvRed = "";

        ishod += "Key1" + separator + "Key2" + separator;
        ishod += "Key3" + separator + "Key4" + separator;

        ishod += "\n";

        foreach (My_Class item in data)
        {
            csvRed += item.Key1+ separator + item.Key2+ separator;
            csvRed += item.Key3+ separator + item.Key4+ separator;

            csvRed += "\n";

            ishod += csvRed;

            csvRed = "";
        }

        return ishod;
    } 

I followed @Clemens advice. 我遵循@Clemens的建议。 I used 我用了

IEnumerable dataList = data.ToList<My_Class>(); 
dataGrid.ItemSource = dataList;

I then called 然后我打电话

exportToCSV(dataList, "\t");

with dataList as parameter and it worked! 与dataList作为参数,它的工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在实体框架中查询长数据类型的“包含”而不枚举结果? - How to query for “Contains” in Entity framework for long datatype without enumerating the result? 如何在不枚举数据的情况下执行异步查询? - How do I do an async query without enumerating the data? 将查询结果另存为csv文件在特定文件夹下的服务器上 - Save Query Result as csv file on server under specific folder 如何在不枚举查询的情况下将字符串转换为int - How can I convert string into int without enumerating a query 如何将csv文件数据保存到数据库? - How to save csv file data to a database? 如何更新/保存数据网格中的数据? - How to Update/Save data in a datagrid? 保存数据记录并再次使用,无需再次查询数据库 - Save data records and use it again without querying again into database 如何在IEnumerable.GroupBy中产生一个匿名类 <T> 在C#LINQ中“即时”(没有枚举结果)? - how to yield an anonymous class in IEnumerable.GroupBy<T> “on the fly” (without enumerating result) in C# LINQ? 如何在 UWP 中将数据从文本框保存到数据网格 - How to save data from textbox to datagrid in UWP 如何在不打开浏览器的情况下运行url并再次保存加载的文件? - how can i run url and save loaded file again without opening browsers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM