extension-methods

Entity framework paging with extension method is slow?

此生再无相见时 提交于 2019-12-19 07:39:18
问题 I have some problems with a slow query in Entity Framework in C#. I have created an extension method called Page to handle paging, but when I use it the query gets really slow. If I just do .Skip(page.Value * pageSize.Value).Take(pageSize.Value) instead of using Page the query gets a lot faster. I guess that doing it with Page fetches all contacts before paging. Is there a way to prevent this or am I doing something else wrong? Query: var contacts = db.Contacts .Where(x => x.AccountID ==

Why can you not invoke extension methods directly?

戏子无情 提交于 2019-12-19 05:22:06
问题 Can someone explain to me why in the following the 3rd invocation of DoSomething is invalid? ( Error message is "The name 'DoSomething' does not exist in the current context" ) public class A { } public class B : A { public void WhyNotDirect() { var a = new A(); a.DoSomething(); // OK this.DoSomething(); // OK DoSomething(); // ?? Why Not } } public static class A_Ext { public static void DoSomething(this A a) { Console.WriteLine("OK"); } } 回答1: Extension methods are still static methods, not

Using extension methods defined in C# from F# code

戏子无情 提交于 2019-12-19 05:18:29
问题 I have a series of extension methods defined for various classes in a C# library. I'm currently writing some F# code and instead of rewriting that code I would simply like to use my existing extension methods in my F# code. I have added a reference to the library and used the open statement to import the namespace, but the extension methods to not appear in F# 回答1: Update: In the current version of F#, you can simply consume extension methods by adding open TheNamespaceOfExtensionMethod to

How to chain views in Django?

我是研究僧i 提交于 2019-12-19 03:59:11
问题 I'm implementing James Bennett's excellent django-contact-form but have hit a snag. My contact page not only contains the form, but also additional flat page information. Without rewriting the existing view the contact form uses, I'd like to be able to wrap, or chain, the views. This way I could inject some additional information via the context so that both the form and the flat page data could be rendered within the same template. I've heard it mentioned that this is possible, but I can't

Can you add extension methods that you call like static methods?

天涯浪子 提交于 2019-12-19 03:44:59
问题 According to Microsoft, "Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type". Is there a way to add an extension method that it called as if it was a static method? Or to do something else that has the same effect? Edit: By which I mean "called as if it was a static method on the extended class". Sorry for the ambiguity. 回答1: According to Microsoft, "Extension methods are a special kind of static method, but they

Generate Extension Methods using System.CodeDom

依然范特西╮ 提交于 2019-12-19 03:19:35
问题 Has anyone ever tried to generate extension methods using System.CodeDom under .NET 4.0? There doesn't seem to be any way to specify a CodeMemberMethod or CodeParameterDeclarationExpression as being an extension method/parameter. If this isn't possible, are there any good workarounds? Thanks 回答1: Apparently CodeDom isn't able to generate the correct code for the first parameter of an extension method, but you can cheat it like this: var param = new CodeParameterDeclarationExpression("this

Extension method resolution

瘦欲@ 提交于 2019-12-19 02:47:09
问题 I wrote an extension method for String to get a char argument, string.Remove(char) . But when I used this, it instead called the default string.Remove(int) method. Shouldn't the presence of an actual method have higher priority than an implicit conversion? 回答1: Instance methods have priority over extension methods. Your observation is proof of the same. When resolving which method to call, it will always pick a matching instance method over an extension method... which is intuitive in a way.

How to define an extension method in a scriptcs csx script

拥有回忆 提交于 2019-12-18 18:53:21
问题 I'm playing with ScriptCS (which is awesome!) but I couldn't figure out how to define an extension method within a .csx script file . Take this example: using System.IO; public static class Extensions { public static string Remove(this string source, params string[] toRemove) { foreach(var r in toRemove) { source = source.Replace(r,""); } return source; } } string[] entries = Directory .GetFiles( @"C:\Users\blah\blah", "*.mp4", SearchOption.AllDirectories) .Select( p => p.Remove("Users"))

evaluating cost/benefits of using extension methods in C# => 3.0 [closed]

橙三吉。 提交于 2019-12-18 16:46:50
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . In what circumstances (usage scenarios) would you choose to write an extension rather than sub-classing an object ? < full disclosure : I am not an MS employee; I do not know Mitsu Furota personally; I do know the author of the open-source Componax library mentioned here, but

Where is the ToList() method? (IQueryable)

做~自己de王妃 提交于 2019-12-18 14:55:08
问题 If I try this, it will work: var query = myContextObject.Users.Where(u=>u.Name == "John"); query.ToList(); I'm able to call ToList and a lot of other extension methods. But if I try this: public List ConvertQueryToList(IQueryable query) { return query.ToList(); } ToList won't be accessible, I'm guessing this is because ToList is an extension method, but then how is that ToList is attached in the first example? Is it possible to access ToList in the second case? 回答1: You need to write it as: