Use LINQ syntax in a fluent style.

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
45
Programming Experience
Beginner
Hello, I have such code:

C#:
var collectionOfClientsDeals = new List<DealMonGoDto>();
foreach (var item in collectionOfDealsOfClient)
                    {
                        foreach (var element in item.Offers)
                        {
                            if (element.Client.Id == indefiniteActor)
                            {
                                var dealOfClient = new DealMonGoDto();

                                collectionOfClientsDeals.Add(dealOfClient);
                            }
                        }
                    }

I can transform it to:
C#:
var collectionOfClientsDeals =
                        (from item in collectionOfDealsOfClient
                         from element in item.Offers
                         where element.Client.Id == indefiniteActor
                         select new DealMonGoDto())
                        .ToList();

And it actually correct, but I try the same in a fluent style like:

C#:
var results = collectionOfDealsOfClient.ForEach(x=>x.Offers.Where(c => c.Client.Id == indefiniteActor).Select(o=> new DealMonGoDto()).ToList();
And I cannot do it correct.
Could you help me?
 

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
45
Programming Experience
Beginner
I am not sure, but:


C#:
                    var results = collectionOfDealsOfClient.ForEach(item =>
                        collectionOfClientsDeals.AddRange(item.Offers.Where(x => x.Client.Id == indefiniteActor)
                            .Select(o => new DealMonGoDto()))).ToList();
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
Use SelectMany().

C#:
var collectionOfClientsDeals = collectionOfDealsOfClient
                                    .SelectMany(item => item.Offers)
                                    .Where(element => element.Client.Id == indefiniteActor)
                                    .Select(element => new DealMonGoDto())
                                    .ToList();
 
Last edited:

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
45
Programming Experience
Beginner
Unfortunately no, anf this monster works.

C#:
var collectionOfDealsOfClient = _unitOfWork.GetCollection<Deal>("Deal").AsQueryable().ToList();

                        var collectionToReturn = new List<Deal>();

                        foreach (var item in collectionOfDealsOfClient)
                        {
                            foreach (var element in item.Offers)
                            {
                                if (element.Client.ClientId == testGuid)
                                {
                                    var dealOfClient = new Deal();

                                    dealOfClient.ClientId = item.ClientId;
                                    dealOfClient.ShifterId = item.ShifterId;
                                    dealOfClient.Offers = item.Offers;
                                    dealOfClient.DealPipeLine = item.DealPipeLine;
                                    dealOfClient.Shifter = item.Shifter;
                                    dealOfClient.TripId = item.TripId;
                                    dealOfClient.DealDate = item.DealDate;
                                    dealOfClient.TotalSum = item.TotalSum;
                                    dealOfClient.Description = item.Description;

                                    collectionToReturn.Add(dealOfClient);
                                }
                            }
                        }
                        
                        _ret = collectionToReturn;
And I don't know how to use your sample.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
In your original code, you didn't show that you needed to access the outer collection.

Anyway, here's s something that will work. It uses the newer C# support for Tuples. If you don't have that, then you'll need to create the tuples explicitly by calling new Tuple(deal, offer).
C#:
var x = collectionOfDealsOfClient
    .SelectMany(deal => deal.Offers, (deal, offer) => (deal, offer))
    .Where(tuple => tuple.offer.Id == testGuid)
    .Select(g => new Deal
            {
                ClientId = g.deal.ClientId,
                ShifterId = g.deal.ShifterId,
                DealPipeLine = g.deal.DealPipeLine,
                Shifter = g.deal.Shifter,
                TripId = g.deal.TripId,
                DealDate = g.deal.DealDate,
                TotalSum = g.deal.TotalSum,
                Descrption = g.deal.Descrption,
                Offers = g.deal.Offers
            })
    .ToList();

Do you really need to create a new instance of Deal?
 

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
45
Programming Experience
Beginner
Super knowledge from your side. Thanks a lot again!

C#:
var x = collectionOfDealsOfThisClient
                                .SelectMany(deal => deal.Offers, (deal, offer) => (deal, offer))
                                .Where(tuple => tuple.offer.Client.ClientId == testGuid)
                                .Select(g => new Deal
                                {
                                    ClientId = g.deal.ClientId,
                                    ShifterId = g.deal.ShifterId,
                                    DealPipeLine = g.deal.DealPipeLine,
                                    Shifter = g.deal.Shifter,
                                    TripId = g.deal.TripId,
                                    DealDate = g.deal.DealDate,
                                    TotalSum = g.deal.TotalSum,
                                    Description = g.deal.Description,
                                    Offers = g.deal.Offers
                                })
                                .ToList();
It was very hard to me.
 

endofunk

Member
Joined
Jan 7, 2022
Messages
16
Programming Experience
10+
Linq Query Solution:
var collectionOfDealsOfClient = _unitOfWork.GetCollection<Deal>("Deal").AsQueryable().ToList();
var collectionToReturn =
  (from item in collectionOfDealsOfClient
  from element in item.Offers
  where element.Client.ClientID == testGuid
  select new {
    new Deal {
      ClientId = item.ClientId,
      ShifterId = item.ShifterId,
      DealPipeLine = item.DealPipeLine,
      Shifter = item.Shifter,
      TripId = item.TripId,
      DealDate = item.DealDate,
      TotalSum = item.TotalSum,
      Description = item.Description,
      Offers = item.Offers
    }
  }).ToList();

The wrapping of the new deal {...} in a new {...} block is effectively a SelectMany i.e. a Select followed by a flatten operation.
Note: in other functional enabled languages SelectMany is more frequently referred to as FlatMap... Map being the equivalent of Select, and Flat implies that it flattens the result; instead of List<List<Deal>>, it returns List<Deal>.
 
Last edited:
Top Bottom