简体   繁体   中英

how to read the parameter with params keyword and implementing a function

Hi I have here a code snippet. This isn't mine, I saw this in the internet while learning on how to deal with Entity Framework I know it just including (eager load) the navigation properties then return it as IQueryable

What I want to know is:

  1. How do you read this parameter params System.Linq.Expressions.Expression<Func<T, object>>[] includeProperties ?

  2. How do you call or use this function? (Should pass collection, am I right? A little example would be a great help since I learn in a way when I see some demo)

     public IQueryable<Customer> AllIncluding(params System.Linq.Expressions.Expression<Func<T, object>>[] includeProperties) { var query = context.Customers; foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } return query; } 

Any help would be much appreciated. Thanks!

params lets you pass an array parameter as an actual array or an open-ended list of values:

var includes = new Expression<Func<Customer, object>>[] 
    { 
        i => i.SubProperty1, 
        i => i.SubProperty2
    };
var query = db.Entities.AllIncluding(includes);

or just

var query = db.Entities.AllIncluding(i => i.SubProperty1, i => i.SubProperty2);

I'm guessing on the specific types and properties, but hopefully you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM