2

DBContext で最新の Entity Framework を使用しています。コンマ区切りの値に変換したい結果セットがあります。VB DataTable to CSV extractの DataTables で同様のことを行いました。QuoteName メソッドが機能しています。また、foreach を使用して動作する GetCSV メソッドの派生物も取得しました。問題は、DataTable の同等のコードよりもかなり遅いことです。だから、誰かがいくつかの提案をしてくれることを願っています。

    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;
    }
4

3 に答える 3

3

ファイルの作成に文字列を使用しないでください。StringBuilder クラスを使用してみてください ( http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx )

文字列は不変オブジェクトです。つまり、文字列が作成されると、変更できなくなります。文字列を変更する (連結するなど) たびに、実際にはまったく新しい文字列を作成しています。ここで文字列を使用するのは非常に非効率的です。

代わりに、stringbuilder オブジェクトを作成します。

StringBuilder builder = new StringBuilder();

builder.Append("my data");

最後は電話するだけ

builder.ToString();
于 2013-04-23T20:14:15.670 に答える
0

これについては、弟に助けてもらいました。彼は StringBuilder も使用すると言いました。

これはコードの答えです:

    /// <summary>
    /// Quotes a string using the following rules:
    /// <list>
    /// <listheader>Rules</listheader>
    /// <item>if the string is not quoted and the string contains the separator string</item>
    /// <item>if the string is not quoted and the string begins or ends with a space</item>
    /// <item>if the string is not quoted and the string contains CrLf</item>
    /// </list>
    /// </summary>
    /// <param name="s">String to be quoted</param>
    /// <param name="quote">
    /// <list>
    /// <listheader>quote characters</listheader>
    /// <item>if len = 0 then double quotes assumed</item>
    /// <item>if len = 1 then quote string is doubled for left and right quote characters</item>
    /// <item>else first character is left quote, second character is right quote</item>
    /// </list>
    /// </param>
    /// <param name="sep">separator string to check against</param>
    /// <returns></returns>
    /// <remarks></remarks>
    public static string QuoteName(this string s, string quote = null, string sep = ",")
    {
        quote = quote == null ? "" : quote;
        switch (quote.Length)
        {
            case 0:
                quote = "\"\"";
                break;
            case 1:
                quote += quote;
                break;
        }
        // Fields with embedded sep are quoted
        if ((!s.StartsWith(quote.Substring(0, 1))) && (!s.EndsWith(quote.Substring(1, 1))))
            if (s.Contains(sep))
                s = quote.Substring(0, 1) + s + quote.Substring(1, 1);
        // Fields with leading or trailing blanks are quoted
        if ((!s.StartsWith(quote.Substring(0, 1))) && (!s.EndsWith(quote.Substring(1, 1))))
            if (s.StartsWith(" ") || s.EndsWith(" "))
                s = quote.Substring(0, 1) + s + quote.Substring(1, 1);
        // Fields with embedded CrLF are quoted
        if ((!s.StartsWith(quote.Substring(0, 1))) && (!s.EndsWith(quote.Substring(1, 1))))
            if (s.Contains(System.Environment.NewLine))
                s = quote.Substring(0, 1) + s + quote.Substring(1, 1);
        return s;
    }

    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);
        var sb = new StringBuilder();
        int iCols = props.Count();

        try
        {
            sb.Append(string.Join(",", Enumerable.Range(0, iCols).Cast<int>().
                Select(ii => props[ii].Name.QuoteName("[]")).ToArray()));

            foreach (var dr in entity)
            {
                sb.AppendLine();
                sb.Append(string.Join(",", Enumerable.Range(0, iCols).Cast<int>().
                    Select(ii => props[ii].GetValue(dr).
                        ToString().QuoteName("\"\"", ",")).ToArray()));
            }
        }
        catch (Exception ex)
        {

            throw;
        }
        return sb.ToString();
    }
}

これが他の誰かに役立つことを願っています。

于 2013-04-24T13:35:25.863 に答える