Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easly convert linq result to Business Object Collection <T>

I have Business Object Collection

I'd like to filter rows using linq, but noticed it returns IEnumerable what can not be cast then to my BOC

E.g I cannot do that

BOC <Client> bocCLients = (BOC <Client>) 
                          from C in ClientsColl where C.ClientId == 100 select C

I've resolved that by looping by linq results and adding returned object to my original collection.

I wonder if there is simpler way?

like image 461
Maciej Avatar asked Dec 31 '25 07:12

Maciej


1 Answers

var bocCLients = ClientsColl.Where(c => c.ClientId == 100).ToList();

Or

var bocCLients = new BOC<Client>(ClientsColl.Where(c => c.ClientId == 100));

Edit Or maybe an AddRange extension

public static void AddRange<T>(this ICollection<T> colSource, IEnumerable<T> collection)
        {
            if (colSource is List<T>)
                ((List<T>)colSource).AddRange(collection); //If List use build in optimized AddRange function
            else
            {
                foreach (var item in collection)
                    colSource.Add(item);
            }
        }
like image 176
Magnus Avatar answered Jan 02 '26 08:01

Magnus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!