Examples of common Linq expressions

When I write code, I begin to identify the places where I could use Linq. My problem is that I'm still very new to the syntax. I know the examples best, but I don’t think it’s easy for me to find the example I need.

I wanted to run this thread to create a repository of common Linq expressions that others might stumble upon on Google.

Question: Can you provide examples of Linq expressions that you use for common tasks?

For example, I already wrote the following:

  • list search
  • array summation
  • summation of some property in a set of elements

To start the thread, I will send a response containing them.

+3
source share
3

tboShippingLotNumber existingLotNumber =
    (tboShippingLotNumber)(from lot in shipmentDetailLine.LotNumbers
                           where 
                               (lot.LotNumber == lotNumber) &&
                               (lot.ShipLineKey == shipmentDetailLine.Key)
                           select lot).ElementAtOrDefault(0);

if (existingLotNumber == null)
{
    // not found exception
}

:

decimal[] array = { 1.5m, 2.5m, 3.5m };
decimal sum = array.Sum();

decimal sum = (from shipment in _ShipmentData.Shipments select 
    shipment.AmountShipped).Sum()
0
0
source

Source: https://habr.com/ru/post/1725871/


All Articles