繁体   English   中英

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

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

嗨,我这里有一个代码片段。 这不是我的,在学习如何处理Entity Framework我在互联网上看到了这个,我知道它只包含(急于加载)导航属性,然后将其返回为IQueryable

我想知道的是:

  1. 您如何阅读此参数params System.Linq.Expressions.Expression<Func<T, object>>[] includeProperties

  2. 您如何调用或使用此功能? (应该通过收集,对吗?举个小例子会很有帮助,因为我在看一些演示时会以某种方式学习)

     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; } 

任何帮助将非常感激。 谢谢!

params使您可以将数组参数作为实际数组或值的开放式列表传递:

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

要不就

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

我正在猜测具体的类型和属性,但希望您能理解。

暂无
暂无

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

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