question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IInsertDataSyntax Rows(IEnumerable)

See original GitHub issue

The idea is to turn this:

Insert.IntoTable("TableName")
.Row(new { SomeColumn = "test1" })
.Row(new { SomeColumn = "test2" });

into this:

Insert.IntoTable("TableName")
.Rows(new [] {
  { SomeColumn = "test1" },
  { SomeColumn = "test2" }
});

The actual use case? Piping CsvHelper GetRecords<T>() results directly into Insert.IntoTable("TableName").Rows(csvReader.GetRecords<T>()

[Migration(1)]
public class M1_MigrationTest : ForwardOnlyMigration
{
  public void Up()
  {
     CsvHelper.Configuration.CsvConfiguration config = new CsvHelper.Configuration.CsvConfiguration();
     config.RegisterClassMap<PersonRecordMap>();

     using (var reader = new CsvHelper.CsvReader(
        new System.IO.StreamReader(GetPersonDataAsEmbeddedResourceStream()),
        config))
     {
        Insert.IntoTable("Person").Rows(reader.GetRecords<PersonRecordMap>().ToList());
     }
  }

  private System.IO.Stream GetPersonDataAsEmbeddedResourceStream()
  {
    /* etc */
  }
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
fubar-codercommented, Oct 17, 2019

Counter suggestion:

reader.GetRecords<PersonRecordMap>().
    .Aggregate((IInsertDataSyntax)Insert.IntoTable("Person"), (pv, item) => pv.Row(item))

OK, ok, definitely not as nice as having:

Insert.IntoTable("Person").Rows(reader.GetRecords<PersonRecordMap>().ToList());

IOW: I agree with this proposal.

0reactions
MihailsKuzminscommented, Mar 20, 2021

Is it possible to use System.ComponentModel.DataAnnotations.Schema.ColumnAttribute? Or any other custom attribute with the same functionality will do.

Currently ExtractData(object) uses the name of a property, but it might be possible that the name of a column differs from the property name.

https://github.com/fluentmigrator/fluentmigrator/blob/9c87d698b161b3a1bb596a5624c02891a64459c5/src/FluentMigrator/Builders/Insert/InsertDataExpressionBuilder.cs#L74-L86

By the way, my implementation as an extension method:

public static IInsertDataSyntax Rows<T>(this IInsertDataOrInSchemaSyntax @this, IEnumerable<T> rows)
{
	var props = typeof(T).GetProperties()
		.ToImmutableDictionary(x => x.GetOneAttribute<ColumnAttribute>().Name ?? x.Name);

	foreach (var row in rows)
	{
		var input = new Dictionary<string, object?>(props.Count);

		foreach (var (col, prop) in props)
			input.Add(col, prop.GetValue(row));

		@this.Row(input);
	}

	return @this;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to add an item to an IEnumerable
IEnumerable is an interface. You can't use Add() on an IEnumerable because they're not required to implement an Add() method.
Read more >
Enumerable.Append<TSource>(IEnumerable ...
A sequence of values. ... The value to append to source . Returns. IEnumerable<TSource>. A new sequence that ends with element . Exceptions....
Read more >
Bulk insert data — ClosedXML 0.102.0 documentation
A method used to insert the values from a generic IEnumerable<> is the IXLCell.InsertData(IEnumerable data) , despite the parameter not being generic ...
Read more >
EnumerableRowCollection<TRow> Class (System.Data)
Determines whether any element of a sequence satisfies a condition. Append<TSource>(IEnumerable<TSource>, TSource). Appends a value to the end of the sequence.
Read more >
Bulk Copy (Bulk Insert)
Some database servers provide functionality to insert large amounts of data ... IEnumerable<T> source) BulkCopyRowsCopied BulkCopy<T>(this DataConnection ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found