简体   繁体   English

如何使用params关键字读取参数并实现函数

[英]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 这不是我的,在学习如何处理Entity Framework我在互联网上看到了这个,我知道它只包含(急于加载)导航属性,然后将其返回为IQueryable

What I want to know is: 我想知道的是:

  1. How do you read this parameter params System.Linq.Expressions.Expression<Func<T, object>>[] includeProperties ? 您如何阅读此参数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: params使您可以将数组参数作为实际数组或值的开放式列表传递:

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. 我正在猜测具体的类型和属性,但希望您能理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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