Linq for switch case.

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
55
Programming Experience
Beginner
Is this possible to rewrite this with linq?
C#:
var collection = await db.Clients.Where(element
                            => element.ClientTypeId == 1 ||
                               element.ClientTypeId == 4 ||
                               element.ClientTypeId == 5 && idsOfClients.Contains(element.ClientId)).ToListAsync();

                    var baseCollection = new List<BaseClientTypeBusinessModel>();

                    foreach (var item in collection)
                    {
                        if (item.ClientTypeId == 1)
                        {
                            var model = _mapper.Map<ClientJuridicalBusinessModel>(item);
                            baseCollection.Add(model);
                        }
                        else if (item.ClientTypeId == 4)
                        {
                            var model = _mapper.Map<ClientPhysicalBusinessModel>(item);
                            baseCollection.Add(model);
                        }
                        else if (item.ClientTypeId == 5)
                        {
                            var model = _mapper.Map<ClientIndividualEntrepreneurBusinessModel>(item);
                            baseCollection.Add(model);
                        }
                    }
 

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
55
Programming Experience
Beginner
C#:
var collection = await db.Clients.Where(element
                        => element.ClientTypeId == 1 && idsOfClients.Contains(element.ClientId) ||
                           element.ClientTypeId == 4 && idsOfClients.Contains(element.ClientId) ||
                           element.ClientTypeId == 5 && idsOfClients.Contains(element.ClientId)).ToListAsync();

                    var baseCollection = new List<BaseClientTypeBusinessModel>();

                    collection.ForEach(item => baseCollection.Add(
                        item.ClientTypeId == 1 ? (BaseClientTypeBusinessModel)_mapper.Map<ClientJuridicalBusinessModel>(item) :
                        item.ClientTypeId == 4 ? (BaseClientTypeBusinessModel)_mapper.Map<ClientPhysicalBusinessModel>(item) :
                        item.ClientTypeId == 5 ? (BaseClientTypeBusinessModel)_mapper.Map<ClientIndividualEntrepreneurBusinessModel>(item) : default)
                    );

                    return baseCollection;
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
I would take advantage of C#'s newer language feature like the switch expression.

C#:
var baseCollection = clients.Where(c => idsOfClients.Contains(c.ClientId))
                            .Select(c => (BaseClientTypeBusinessModel)(c.ClientTypeId switch
                                    {
                                        1 => _mapper.Map<ClientJuridicalBusinessModel>(c),
                                        4 => _mapper.Map<ClientPhysicalBusinessModel>(c),
                                        5 => _mapper.Map<ClientIndividualEntrepreneurBusinessModel>(c),
                                        _ => null
                                    }))
                            .Where(b => b != null)
                            .ToList();
 
Last edited:
Top Bottom