简体   繁体   中英

Extending linq (where to place code)

I came across an article that describes how to create a .Each() for LINQ, a la jQuery. I stuck the code in one of my controllers but when I try to call it, Intellisense doesn't see it... I also read an article from 4guysFromRolla that describes how to create LINQ extensions but they don't talk about having to place the code in any special place...

what's the magic here?

Extension methods belong into separate static class.

From linked article:

The following example shows an extension method defined for the System.String class. Note that it is defined inside a non-nested, non-generic static class:

You should also add using to namespace of this class.

扩展方法应在非嵌套,非通用静态类内定义: 扩展方法

You can place it anywhere but it would be wise to move it to a separate Tools folder+ns or even to an assembly.

A basic idea: create a public static class MyLinqExtensions in an \\MyExtensions folder.

Replace 'My' with something Project specific.

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