how can I reduce 2 select to 1 select linq to gain performance?

Penguen

my question is simple but I got stuck with something. Can you tell me how can I reduce 2 select into 1 select LINQ in c#? I am using CloudNative.CloudEvents NuGet package for cloud-native events.

var orderEvents = input
    .Select(_ => new OrderDocument(_.Id, _.ToString()).ToOrderEvent())
    .Select(_ =>
        new CloudEvent()
        {
            Type = _.EventType,
            Subject = _.Subject,
            Source = _.Source,
            Data = _
        });

input is a parameter from cosmosDbTrigger it`s type : IReadOnlyList

OrderDocument.cs

public class OrderDocument
{
    public string Id { get; private set; }

    public string Json { get; private set; }

    public OrderDocument(string id, string json)
    {
        Id = id;
        Json = json;
    }

    public OrderEvent ToOrderEvent() => OrderEventHelper.ToOrderEvent(Json);
}

OrderEventHelper.cs

public static OrderEvent ToOrderEvent(string json)
{
    ArgumentHelper.ThrowIfNullOrEmpty(json);

    var orderEvent = JsonConvert.DeserializeObject<OrderEvent>(json);
    var eventDefinition = OrderEvents.EventDefinitions.SingleOrDefault(_ => _.EventType == orderEvent.EventType);

    
    return eventDefinition == null
        ? orderEvent
        : new OrderEvent(
            orderEvent.Id,
            orderEvent.Source,
            orderEvent.EventType,
            orderEvent.Subject,
            orderEvent.DataContentType,
            orderEvent.DataSchema,
            orderEvent.Timestamp,
            JsonConvert.DeserializeObject(orderEvent.Payload.ToString(), eventDefinition.PayloadType),
            orderEvent.TraceId);

}
Ibrennan208

linq extensions are basically for loops in the background. If you want to perform multiple actions against a list, perhaps making your own simple for loop where you can manage that yourself would work.

Your code:

var orderEvents = input
    .Select(_ => new OrderDocument(_.Id, _.ToString()).ToOrderEvent())
    .Select(_ =>
        new CloudEvent()
        {
            Type = _.EventType,
            Subject = _.Subject,
            Source = _.Source,
            Data = _
        });

could be changed to:

// our result set, rather than the one returned from linq Select
var results = new List<CloudEvent>();

foreach(var x in input){
   // create the order event here
   var temporaryOrderEvent = new OrderDocument(x.Id, x.ToString()).ToOrderEvent();
   
   // add the Cloud event to our result set
   results.Add(new CloudEvent() 
   {
       Type = temporaryOrderEvent .EventType,
       Subject = temporaryOrderEvent .Subject,
       Source = temporaryOrderEvent .Source,
       Data = temporaryOrderEvent 
   });
}

where you then have a result list to work with.

If you wanted to keep it all in linq, you could instead perform all of your logic in the first Select, and ensure that it returns a CloudEvent. Notice here that you can employ the use of curly brackets in the linq statement to evaluate a function rather than a single variable value:

var orderEvents = input
    .Select(x => 
    {
       // create the order event here
       var temporaryOrderEvent = new OrderDocument(x.Id, x.ToString()).ToOrderEvent();

       // return the Cloud event here
       return new CloudEvent() 
                  {
                       Type = temporaryOrderEvent .EventType,
                       Subject = temporaryOrderEvent .Subject,
                       Source = temporaryOrderEvent .Source,
                       Data = temporaryOrderEvent 
                  };
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I Select an IEnumerable over 2 Tables using LINQ

In a LINQ select how can I select the FirstOrDefault of a list?

How can I do a select following a select in LINQ

Can I customize a Select in Linq to select into an object?

How can I add a .Where into a .Select into an object using LINQ?

How can I select with a GroupBy into an anonymous class with LINQ?

How can I select the most recent and distinct records using LINQ?

How can i select all values from a nested dictionary with linq?

How can I select the first child of a child using linq?

How can I get a value inside the select query using linq?

How can I set the value of a parameter in a Select using LINQ?

How I can select two elements to one list with linq

How can i improve the performance of this LINQ?

Linq Select into New Object Performance

MYSQL - How can I select a value from either table 1 or table 2?

Linq2SQL Select in interface for better performance

How can I group and select 1 row or if there's more than 1 just the last rows with a SQL SELECT

how can I select 1 item by index the list in a generator result?

How can I select the H1 tag?

How can I select only 1 word with formatBlock

How can i select 1 checkbox in each card by using javascript

How do I do an inner select in linq?

How can I select this button?

How can I select all filter item in performance point service dashboard designer?

How can I get an output by click on a select2 element?

How can I use multiple modified select2 selectboxes?

How can I catch the width of select2 dropdown?

How can I use a select2 selectbox with dataTables?

How can I select a table that have 2 part name(Mysql?