none
dot net core property loop syntax issue RRS feed

  • Question

  • Hi Team,

    please help , I would like to know what is the syntax for looping in dot net core 2.0

      var tvp = new TableValuedParameterBuilder("dbo.IntTable", new SqlMetaData("val", SqlDbType.Int))
                      .AddRow(64603)
                      .AddRow(64601) // I want to add this value runtime and I want to add more rows here runtime
                      .AddRow(64602)
                      .CreateParameter("DealID");

    How can I make .Add row property dynamic ? I want to add more .Addrow runtime to above code

    // general common class

    public class TableValuedParameterBuilder
        {
            readonly string _typeName;
            readonly SqlMetaData[] _columns;
            readonly List<SqlDataRecord> _rows;

            public TableValuedParameterBuilder(string typeName, params SqlMetaData[] columns)
            {
                _typeName = typeName;
                _columns = columns;
                _rows = new List<SqlDataRecord>();
            }

            public TableValuedParameterBuilder AddRow(params object[] fieldValues)
            {
                var row = new SqlDataRecord(_columns);
                row.SetValues(fieldValues);
                _rows.Add(row);
                return this;
            }

            public SqlParameter CreateParameter(string name)
            {
                return new SqlParameter
                {
                    ParameterName = name,
                    Value = _rows,
                    TypeName = _typeName,
                    SqlDbType = SqlDbType.Structured
                };
            }
        }

    please guide.

    Thanks & Regards,

    Pritesh


    Sunday, September 23, 2018 6:54 PM

Answers

  • hello, 

    If I understood you want to call the AddRow in a loop to add the list of rows definitions you want using reflection, if so here is a code snippet that I used to call Console.WriteLine() : 

    using System.Reflection;
    
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        Type type = typeof(Console);
        var properties = type.GetProperties();
        var writeLineMethod = type.GetMethod("WriteLine", new Type[]{ typeof(String)});
        var writeLineDelegate = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), null, writeLineMethod);
        writeLineDelegate("Test");
        Console.ReadLine();
    }

    let do the same for your object :

    using System.Reflection;
    ....
    
    var tvp = new TableValuedParameterBuilder("dbo.IntTable", new SqlMetaData("val", SqlDbType.Int));
    var addRowMethod = typeof(TableValuedParameterBuilder).GetMethod("AddRow");
    var addRowDelegate = (Func<object[],TableValuedParameterBuilder>)Delegate.CreateDelegate(typeof(Func<object[],TableValuedParameterBuilder>), tvp, addRowMethod);
    
    // ......
    
    foreach(var fieldsValues in rowsValues){
       addRowDelegate(fieldsValues);
    }

    you can also do it the simple way :

    foreach(var fieldsValues in rowsFieldsValues){
        tvp.AddRow(fieldsValues);
    }

    Good coding;

    • Marked as answer by Pritesh_Dabhi Thursday, September 27, 2018 11:16 PM
    Sunday, September 23, 2018 11:03 PM

All replies

  • Check this:

       var b = new TableValuedParameterBuilder( … );
       b = b
               
    .AddRow(…)
              .AddRow(…);
       for( … )
       {
          b = b.AddRow(…);
       }
       if( … )
       {
          b = b.AddRow(…);
       }
       var tvp = b.CreateParameter( … );






    • Edited by Viorel_MVP Sunday, September 23, 2018 7:44 PM
    • Proposed as answer by laurens vdb Sunday, September 23, 2018 7:54 PM
    Sunday, September 23, 2018 7:40 PM
  • hello, 

    If I understood you want to call the AddRow in a loop to add the list of rows definitions you want using reflection, if so here is a code snippet that I used to call Console.WriteLine() : 

    using System.Reflection;
    
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        Type type = typeof(Console);
        var properties = type.GetProperties();
        var writeLineMethod = type.GetMethod("WriteLine", new Type[]{ typeof(String)});
        var writeLineDelegate = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), null, writeLineMethod);
        writeLineDelegate("Test");
        Console.ReadLine();
    }

    let do the same for your object :

    using System.Reflection;
    ....
    
    var tvp = new TableValuedParameterBuilder("dbo.IntTable", new SqlMetaData("val", SqlDbType.Int));
    var addRowMethod = typeof(TableValuedParameterBuilder).GetMethod("AddRow");
    var addRowDelegate = (Func<object[],TableValuedParameterBuilder>)Delegate.CreateDelegate(typeof(Func<object[],TableValuedParameterBuilder>), tvp, addRowMethod);
    
    // ......
    
    foreach(var fieldsValues in rowsValues){
       addRowDelegate(fieldsValues);
    }

    you can also do it the simple way :

    foreach(var fieldsValues in rowsFieldsValues){
        tvp.AddRow(fieldsValues);
    }

    Good coding;

    • Marked as answer by Pritesh_Dabhi Thursday, September 27, 2018 11:16 PM
    Sunday, September 23, 2018 11:03 PM
  • Thank you Sir.

    Regards,

    Pritesh

    Thursday, September 27, 2018 11:17 PM
  • Thank you for your kind reply Sir, it was difficult for me to understand code you suggest.

    Regards,

    Pritesh

    Thursday, September 27, 2018 11:18 PM
  • Welcome, 

    Regards, 

    Happy to help;


    Thursday, September 27, 2018 11:19 PM