How to exclude objects from an array of objects by criteria.

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
To be short:

Have model:

C#:
public class DealsOffersParcelsUnitedDto
    {
        public Guid? ShifterId { get; set; }

        [BsonElement("offers")]
        public List<Offer>? OfferCharacteristicsList { get; set; }
        public DealStatuses DealStatus { get; set; }
        public string? DescriptionOfLoad { get; set; }

        //Parcel
        public Guid? ClientId { get; set; }
        public DateTime Date { get; set; }
        public GeoPoint? Location { get; set; }
        public Address? AddressFrom { get; set; }
        public Address? AddressTo { get; set; }
        public UserAuthInfo? Client { get; set; }
        public BaggageType[]? BaggageTypes { get; set; }
        public Transport Transport { get; set; }
        public int Sum { get; set; }
        public string? Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public string? OfferId { get; set; }
        public bool IsActive { get; set; } = false;

        public string? LastModified { get; set; }
    }


C#:
public class Deal : BaseEntity
    {
        [BsonElement("shifterId")]
        [JsonPropertyName("shifterId")]
        public Guid? ShifterId { get; set; }

        public List<Offer>? Offers;
        public DealStatuses DealStatus { get; set; }
        public string? Description { get; set; }
        public BaggageType[]? BaggageType { get; set; }
        public Transport Transport { get; set; }
        public GeoPoint? Location { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public Address? AddressFrom { get; set; }
        public Address? AddressTo { get; set; }
        public bool IsActive { get; set; } = false;
        public string? LastModified { get; set; }
        
    }
Have query:( thanks to

Skydiver)


C#:
var collectionOfDealsOfTheCurrentClient = await dealCollection
                    .SelectMany(deal => deal.Offers, (deal, offer) => (deal, offer))
                    .Where(tuple => tuple.offer.ClientId == currentUserId)
                    
                    .Select(g => new DealsOffersParcelsUnitedDto()
                    {
                        ShifterId = g.deal.ShifterId,
                        DealStatus = g.deal.DealStatus,
                        Description = g.deal.Description,
                        OfferCharacteristicsList = g.deal.Offers,
                        BaggageTypes = g.deal.BaggageType,
                        Transport = g.deal.Transport,
                        Location = g.deal.Location,
                        StartDate = g.deal.StartDate,
                        EndDate = g.deal.EndDate,
                        AddressFrom = g.deal.AddressFrom,
                        AddressTo = g.deal.AddressTo,
                        IsActive = g.deal.IsActive,
                        LastModified = DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")
            })
                    .ToAsyncEnumerable()
                    .ToList();

but have a response
C#:
[
    {
        "shifterId": "3f7d416f-879d-41be-b411-2a08db977aa6",
        "offerCharacteristicsList": [

            {
                "offerStatus": "ConfirmedDeliveryClient",
                "whoCreator": "ClientFirst",
                "offerSum": 43777.0,
                "clientId": "3f7d416f-879d-41be-b411-3a08db977aa6",
                "description": "Hello, Antonio!",
                "lastModified": "Saturday, 12 February 2022 16:18:12",
                "id": "efbc5955-3467-4bb0-abc4-4f7a9c278bce",
                "createdOn": "2022-02-11T20:32:09.369Z"
            },
            {
                "offerStatus": "ConfirmedDeliveryShifter",
                "whoCreator": "ShifterFirst",
                "offerSum": 400.0,
                "clientId": "81b2e721-bb39-4031-9917-9cf6422237c4",
                "description": "",
                "lastModified": "Saturday, 12 February 2022 16:18:12",
                "id": "bf3c87a4-9a7c-4e94-9b48-2dcd295efa57",
                "createdOn": "2022-02-12T13:04:59.369Z"
            },
         
            {
                "offerStatus": "AgreeAll",
                "whoCreator": "ClientFirst",
                "offerSum": 4000.0,
                "clientId": "3f7d416f-879d-41be-b411-2a08db977aa9",
                "description": "",
                "lastModified": "Saturday, 12 February 2022 17:56:40",
                "id": "4188a2cd-2b68-4d4c-a57c-45fb2acc0944",
                "createdOn": "2022-02-12T14:56:40.549Z"
            }
        ],
        "dealStatus": "OnMyWay",
        "date": "0001-01-01T00:00:00",
        "location": {


clientId not only 3f7d416f-879d-41be-b411-3a08db977aa6.
So, the question is - how to get in response only objects of offer with desired clientId?
 
I try smth like that
C#:
OfferCharacteristicsList = g.deal.Offers.FindAll(x=>x.ClientId==clientId),
or
C#:
OfferCharacteristicsList = g.deal.Offers.Except(x=>x.ClientId!=clientId),

but there is a mistake from the compiler.
I could not find the technique to solve my thread. Please, help me.
 
I think I am tired.

C#:
OfferCharacteristicsList = g.deal.Offers.Where(x => x.ClientId == currentUserId).ToList(),
 
there is a mistake from the compiler.
What is the exact error message or messages you are getting from the compiler? Post it/them verbatim. Don't paraphrase.

What you have in post #3 looks reasonable. Does it work?
 
Back
Top Bottom