[英].Net Entity Framework to CSV
我正在使用最新的Entity Framework和DBContext。 我有一個結果集,我想轉換為逗號分隔值。 我在VB DataTable中使用DataTables做了類似的事情來進行CSV提取 。 我有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;
}
查看nuget包CsvHelper( http://nuget.org/packages/CsvHelper/ )。
以下是https://github.com/JoshClose/CsvHelper/wiki/Basics的用法示例
using (var csv = new CsvWriter( new StreamWriter( "Actors.csv" ) ))
{
csv.WriteRecords( actorsList );
}
不要使用字符串來創建文件。 嘗試使用StringBuilder類( http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx )
字符串是不可變對象 - 也就是說,一旦創建了字符串,就無法更改它。 每次更改字符串(例如連接它)時,您實際上都在創建一個全新的字符串。 在這里使用字符串是非常低效的。
而是創建一個stringbuilder對象:
StringBuilder builder = new StringBuilder();
builder.Append("my data");
最后,只需打電話
builder.ToString();
我得到了我弟弟的幫助。 他說也要使用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();
}
}
我希望這對其他人有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.