none
Concurrent Bag add RRS feed

  • Question

  • Hi Experts, 

    Could any one please share how I can add an item to Concurrent bag for the following requirement. I basically created a class named BagStructure & need to add data to it. am new to C#.

    Am trying to add a value like below which works for Data Table but not sure how to do for Concurrent Bag when I have multiple elements like event_name, object_name, object_id, object_type

    class BagStructure { public string Event_Name { get; set; } public string object_name { get; set; } public Int32 object_id { get; set; } public string object_type { get; set; } } public static void LoadFile(string InputFilePath, string tableName) { var data = new ConcurrentBag<BagStructure>(); var xeStream = new XEFileEventStreamer(InputFilePath); xeStream.ReadEventStream( xevent => { XEextract(xevent, data); return Task.CompletedTask; }, CancellationToken.None).Wait(); } private static void XEextract(IXEvent xe, ConcurrentBag<QueryPerfStructure> data) { xe.Fields.TryGetValue("object_id", out object object_id); data.Add.object_id = object_id == null ? 0 : object_id;

       xe.Fields.TryGetValue("object_name", out object object_name);
                data.Add.["object_name"] = object_name == null ? string.Empty : object_name;

                xe.Fields.TryGetValue("object_type", out object object_type);
                data.Add.["object_type"] = object_type == null ? string.Empty : object_type;

    }

    Thanks Priya

    Saturday, December 21, 2019 8:00 AM

Answers

  • class BagStructure
        {
            public string Event_Name { get; set; }
            public string object_name { get; set; }
            public Int32 object_id { get; set; }
            public string object_type { get; set; }
      }

    You have a class that has public properties. A class must be instanced into an object using the 'new' keyword.

    var bs = new BagStructure();

    You populat data to a object's public property.

    bs.Event_Name = "test";

    You populate the rest of the object's public properties.

    You add the object to the bag.

    data.Add(bs);

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

       



    • Edited by DA924x Saturday, December 21, 2019 9:39 AM
    • Marked as answer by Priya Bange Saturday, December 21, 2019 10:07 AM
    Saturday, December 21, 2019 9:38 AM
  • You can create a generic language extension method

    public static class Extensions
    {
        public static void AddRange<T>(this ConcurrentBag<T> @this, IEnumerable<T> toAdd)
        {
            foreach (var element in toAdd)
            {
                @this.Add(element);
            }
        }
    }

    The above came from the following post with an example.

    https://stackoverflow.com/questions/10177768/concurrentbag-add-multiple-items


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Priya Bange Saturday, December 21, 2019 10:07 AM
    Saturday, December 21, 2019 10:04 AM
    Moderator
  • Thank you for the quick support, If I have multiple public variables defined in the constructor

    You do not have public variables in a class. Those are public properties in a class, which happens to be auto-properties, and the class must be instanced into an object in order  for you to address/access the public properties in the object.

    https://medium.com/omarelgabrys-blog/properties-vs-fields-in-c-6cec86c59dc9

    The link is talking about object instance which is accomplished by the 'new' keyword in C#.

    Class vs instance vs object. It's OO 101 for any OO language platform like Java and.NET

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    You have a simple class/object whose purpose is to carry data by means of using public properties, a container object, which is similar to a  Data Transfer Object.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors

    Your class has no class constructor that I know about. Yes, a container class can have a constructor to populate a private backing variable for a public property.

    data.Add(Event_name,object_name,object_id,object_type)

    No, I don't think that's going to work, becuase you have to deal with the  BagStructure object as a whole or one unit of data.

    The Product object in the link is a container object.

    https://stackoverflow.com/questions/15521584/what-is-the-correct-usage-of-concurrentbag/15528582


    • Edited by DA924x Saturday, December 21, 2019 11:03 AM
    • Marked as answer by Priya Bange Saturday, December 21, 2019 2:36 PM
    Saturday, December 21, 2019 10:55 AM

All replies

  • class BagStructure
        {
            public string Event_Name { get; set; }
            public string object_name { get; set; }
            public Int32 object_id { get; set; }
            public string object_type { get; set; }
      }

    You have a class that has public properties. A class must be instanced into an object using the 'new' keyword.

    var bs = new BagStructure();

    You populat data to a object's public property.

    bs.Event_Name = "test";

    You populate the rest of the object's public properties.

    You add the object to the bag.

    data.Add(bs);

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

       



    • Edited by DA924x Saturday, December 21, 2019 9:39 AM
    • Marked as answer by Priya Bange Saturday, December 21, 2019 10:07 AM
    Saturday, December 21, 2019 9:38 AM
  • class BagStructure
        {
            public string Event_Name { get; set; }
            public string object_name { get; set; }
            public Int32 object_id { get; set; }
            public string object_type { get; set; }
      }

    You have a class that has public properties. A class must be instanced into an object using the 'new' keyword.

    var bs = new BagStructure();

    You populat data to a object's public property.

    bs.Event_Name = "test";

    You populate the rest of the object's public properties.

    You add the object to the bag.

    data.Add(bs);

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

       



    Hello Sir,

    Thank you for the quick support, If I have multiple public variables defined in the constructor 

    like 

    { public string Event_Name { get; set; } public string object_name { get; set; } public Int32 object_id { get; set; } public string object_type { get; set; } }

    How can I add all of them in a single go to the bag something like below 

    data.Add(Event_name,object_name,object_id,object_type)

    Saturday, December 21, 2019 9:49 AM
  • You can create a generic language extension method

    public static class Extensions
    {
        public static void AddRange<T>(this ConcurrentBag<T> @this, IEnumerable<T> toAdd)
        {
            foreach (var element in toAdd)
            {
                @this.Add(element);
            }
        }
    }

    The above came from the following post with an example.

    https://stackoverflow.com/questions/10177768/concurrentbag-add-multiple-items


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Priya Bange Saturday, December 21, 2019 10:07 AM
    Saturday, December 21, 2019 10:04 AM
    Moderator
  • Thank you for the quick support, If I have multiple public variables defined in the constructor

    You do not have public variables in a class. Those are public properties in a class, which happens to be auto-properties, and the class must be instanced into an object in order  for you to address/access the public properties in the object.

    https://medium.com/omarelgabrys-blog/properties-vs-fields-in-c-6cec86c59dc9

    The link is talking about object instance which is accomplished by the 'new' keyword in C#.

    Class vs instance vs object. It's OO 101 for any OO language platform like Java and.NET

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    You have a simple class/object whose purpose is to carry data by means of using public properties, a container object, which is similar to a  Data Transfer Object.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors

    Your class has no class constructor that I know about. Yes, a container class can have a constructor to populate a private backing variable for a public property.

    data.Add(Event_name,object_name,object_id,object_type)

    No, I don't think that's going to work, becuase you have to deal with the  BagStructure object as a whole or one unit of data.

    The Product object in the link is a container object.

    https://stackoverflow.com/questions/15521584/what-is-the-correct-usage-of-concurrentbag/15528582


    • Edited by DA924x Saturday, December 21, 2019 11:03 AM
    • Marked as answer by Priya Bange Saturday, December 21, 2019 2:36 PM
    Saturday, December 21, 2019 10:55 AM