.NET实体框架CSV [英] .Net Entity Framework to CSV

查看:174
本文介绍了.NET实体框架CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是最新的实体框架用的DbContext。我有我要转换为逗号分隔值的结果集。我已经做了在VB中数据表到CSV提取数据表类似的东西。我已经得到了QUOTENAME方法工作。我也得到了GetCSV方法使用foreach工作的衍生物。问题是,它比一个DataTable中的可比代码慢很多。所以,我希望有人将有一些建议。

 公共静态字符串GetCSV(这IQueryable的实体)
$ { b $ b如果(实体== NULL)
{
抛出新的ArgumentNullException(实体);
}
型T = entity.ElementType;
VAR道具= T.GetProperties(BindingFlags.Public | BindingFlags.Instance);
字符串s =的String.Empty;
INT iCols = props.Count();


{
S + =的string.join(,(从int II在Enumerable.Range(0,iCols)
选择道具[II] .Name.QuoteName([]))ToArray的())。
S + = Environment.NewLine;
的foreach(在实体变种DR)
{
S + =的string.join(,(从int II在Enumerable.Range(0,iCols)
选择
道具[Ⅱ] .GetValue(DR)
的ToString()
.QuoteName(\\,,))ToArray的())。
S + = Environment.NewLine;
}
S = s.TrimEnd(新的char [] {(炭)的0x0A,(炭)0X0D});
}
赶上(例外)
{

扔;
}
返回小号;
}


解决方案

不要使用字符串创建您的文件。尝试使用StringBuilder类( http://msdn.microsoft.com /en-us/library/system.text.stringbuilder.aspx



字符串是不可变对象 - 也就是说,一旦创建一个字符串,它不能被改变。每次你改变一个字符串(如串连吧)的时候,你实际上是创建一个全新的字符串。 。使用字符串是非常低效这里



相反,创建一个StringBuilder对象:

  StringBuilder的建设者=新的StringBuilder(); 

builder.Append(我的数据);



最后,只需调用

  builder.ToString(); 


I'm using the latest Entity Framework with DBContext. I have a result set that I want to convert to comma separated values. I've done something similar with DataTables in VB DataTable to CSV extraction. I've got the QuoteName method working. I've also get a derivative of the GetCSV method working using a foreach. The problem is that it is a lot slower than the comparable code for a DataTable. So I'm hoping someone will have some suggestions.

    public static string GetCSV(this IQueryable entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        Type T = entity.ElementType;
        var props = T.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        string s = string.Empty;
        int iCols = props.Count();

        try
        {
            s += string.Join(",", (from int ii in Enumerable.Range(0, iCols)
                                   select props[ii].Name.QuoteName("[]")).ToArray());
            s += Environment.NewLine;
            foreach (var dr in entity)
            {
                s += string.Join(",", (from int ii in Enumerable.Range(0, iCols)
                                       select
                                           props[ii].GetValue(dr)
                                                    .ToString()
                                                    .QuoteName("\"\"", ",")).ToArray());
                s += Environment.NewLine;
            }
            s = s.TrimEnd(new char[] { (char)0x0A, (char)0x0D });
        }
        catch (Exception)
        {

            throw;
        }
        return s;
    }

解决方案

Don't use a string to create your file. Try using the StringBuilder class (http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx)

Strings are immutable objects - that is, once a string is created, it cannot be changed. Every time you change a string (such as concatenate it), you're actually creating a brand new string. Using a string is very inefficient here.

Instead, create a stringbuilder object:

StringBuilder builder = new StringBuilder();

builder.Append("my data");

At the end, just call

builder.ToString();

这篇关于.NET实体框架CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆