extension-methods

How to implement left join in JOIN Extension method

。_饼干妹妹 提交于 2019-11-26 10:33:53
问题 I am trying to implement an outer join on this kind of query for the p.Person table. How would I do this? This example is taken from http://ashishware.com/DSLinqExample.shtml var onlyinfo = p.Person .Where(n => n.FirstName.Contains(\'a\')) .Join(p.PersonInfo, n => n.PersonId, m => m.PersonId, (n, m) => m) .ToArray<Persons.PersonInfoRow>(); 回答1: Normally left joins in LINQ are modelled with group joins, sometimes in conjunction with DefaultIfEmpty and SelectMany : var leftJoin = p.Person.Where

Is it possible to implement mixins in C#?

依然范特西╮ 提交于 2019-11-26 10:17:54
问题 I\'ve heard that it\'s possible with extension methods, but I can\'t quite figure it out myself. I\'d like to see a specific example if possible. Thanks! 回答1: It really depends on what you mean by "mixin" - everyone seems to have a slightly different idea. The kind of mixin I'd like to see (but which isn't available in C#) is making implementation-through-composition simple: public class Mixin : ISomeInterface { private SomeImplementation impl implements ISomeInterface; public void OneMethod(

Detect target framework version at compile time

吃可爱长大的小学妹 提交于 2019-11-26 10:17:14
I have some code which makes use of Extension Methods, but compiles under .NET 2.0 using the compiler in VS2008. To facilitate this, I had to declare ExtensionAttribute: /// <summary> /// ExtensionAttribute is required to define extension methods under .NET 2.0 /// </summary> public sealed class ExtensionAttribute : Attribute { } However, I'd now like the library in which that class is contained to also be compilable under .NET 3.0, 3.5 and 4.0 - without the 'ExtensionAttribute is defined in multiple places' warning. Is there any compile time directive I can use to only include the

Can I use extension methods and LINQ in .NET 2.0 or 3.0?

家住魔仙堡 提交于 2019-11-26 09:56:27
问题 When I try to add an extension method using the .NET 2.0 or 3.0 runtime, I get the error: Cannot define a new extension method because the compiler required type \'System.Runtime.CompilerServices.ExtensionAttribute\' cannot be found. Are you missing a reference to System.Core.dll? But I can\'t find System.Core in the list of available references when I try to add it to the project. What do I need to do to be able to use extension methods and in turn LINQ on in my projects? 回答1: Extension

How can I get an extension method to change the original object?

一个人想着一个人 提交于 2019-11-26 09:45:53
问题 I want to be able to write extension methods so that I can say: lines.ForceSpaceGroupsToBeTabs(); instead of: lines = lines.ForceSpaceGroupsToBeTabs(); However, the following code currently outputs: ....one ........two instead of: Tone TTtwo What do I have to change in the following code to make it output: Tone TTtwo (note that for visibility, . = space, T = \\t ): using System; using System.Collections.Generic; namespace TestExtended82343 { class Program { static void Main(string[] args) {

Extension method on enumeration, not instance of enumeration

空扰寡人 提交于 2019-11-26 09:34:41
问题 I have an enumeration for my Things like so: public enum Things { OneThing, AnotherThing } I would like to write an extension method for this enumeration (similar to Prise\'s answer here) but while that method works on an instance of the enumeration , ala Things thing; var list = thing.ToSelectList(); I would like it to work on the actual enumeration instead: var list = Things.ToSelectList(); I could just do var list = default(Things).ToSelectList(); But I don\'t like the look of that :) I

Why doesn&#39;t Include have any effect?

和自甴很熟 提交于 2019-11-26 08:57:25
问题 I am doing the following LINQ Query which works but doesn\'t return the navigation property Person filled, I get null . public IEnumerable<SharePeople> GetSharePeopeByCarId(int carId) { return from q in _context.Cars join s in _context.Shares on q.CarId equals s.Car.CarId join p in _context.SharePeople.Include(p => p.Person) on s.ShareId equals p.ShareId where q.CarId == carId select p; } I have no idea why, since when I do the regular extension method like _context.SharePeople.Include(p => p

What Advantages of Extension Methods have you found? [closed]

旧时模样 提交于 2019-11-26 08:52:00
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . A \"non-believer\" of C# was asking me what the purpose to extension methods was. I explained that you could then add new methods to

.NET List<T> Concat vs AddRange

佐手、 提交于 2019-11-26 08:15:03
问题 What is the difference between the AddRange and Concat functions on a generic List? Is one recommended over the other? 回答1: They have totally different semantics. AddRange modifies the list by adding the other items to it. Concat returns a new sequence containing the list and the other items, without modifying the list. Choose whichever one has the semantics you want. 回答2: The big difference is that AddRange mutates that list against which it is called whereas Concat creates a new List. Hence

Swift extension example

青春壹個敷衍的年華 提交于 2019-11-26 07:55:26
问题 I was originally wanting to know how to make something like this UIColor.myCustomGreen so that I could define my own colors and use them throughout my app. I had studied extensions before and I thought that I could probably use them to solve my problem, but I couldn\'t remember exactly how to set extensions up. Searching on Google at the time of this writing for \"Swift extension\" resulted in the documentation, several long tutorials, and a rather unhelpful Stack Overflow question. So the