extension-methods

Extending IEnumerable to Return BindingList

陌路散爱 提交于 2019-12-24 14:16:17
问题 In a previous question on Stack Overflow, I had run into an issue with returning an EF query to the DataGridView. Of course I'd run into an issue. However, I added an extension method that still has me baffled since it isn't working. It seems like it should, but for some reason it's not. public static class BindingListEntityExtension { public static BindingList<T> ToBindingList<T>(this IEnumerable<T> entities) { BindingList<T> rtn = new BindingList<T>(); foreach (T obj in entities) { rtn.Add

Expressions definition clarification

拟墨画扇 提交于 2019-12-24 13:12:15
问题 Could anyone provide a clear (and easy to understand) explanation of what happens here (generally, generics, extension methods and Expression all together): public static MvcHtmlString TextBoxFor<TModel, TProperty> (this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) { return htmlHelper.TextBoxFor(expression, format: null); } and further using of it here: Html.TextBoxFor(o => Model.SomeValue) The most problematic moment for the understanding is the way how

Does AutoMapper's convention based mappings work with LINQ extension methods?

守給你的承諾、 提交于 2019-12-24 11:29:32
问题 I apologize if this is a duplicate but I did not find anything that seemed to be matching up with what I am looking for. As we all know in Automapper we can perform convention based mappings... My Question Is it possible to access extension methods (LINQ.First()) on objects in a collection, to go "n" levels deep? See example below My Entities public class Store { public IList< Departments > Departments {get;set;} } public class Departments { public bool Open {get;set;} } What I want to be

How do I port an extension in VB.NET to C#?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 09:21:00
问题 I wrote an extension in VB.NET for StringBuilder to add a AppendFormattedLine method (so I would not have to use one of the arguments for a new line character): Imports System.Runtime.CompilerServices Public Module sbExtension <Extension()> _ Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _ ByVal format As String, _ ByVal arg0 As Object) oStr.AppendFormat(format, arg0).Append(ControlChars.NewLine) End Sub <Extension()> _ Public Sub AppendFormattedLine(ByVal oStr As

List files name and path in worksheet for specific dir and character count

点点圈 提交于 2019-12-24 07:45:37
问题 I've tried and search through out vba forum to figure out how can I rectify my code (below) to search files within a specific directory and its sub-directories to list and populated list of file that have 20 characters in filename length and just only pdf extension. I want to list of file with no extension at the end in column A and full file path and name in column B. Also tried to sort all files ascending after list created but no success yet :( any help? Thanks Sub ListPDF() Range("A:L")

Getting a generic method to infer the type parameter from the runtime type

旧时模样 提交于 2019-12-24 04:19:25
问题 I have some extension methods for an abstract base class, let's say Pet , that themselves call a generic method that takes in that type parameter. public static Pet_ExtensionMethods { public static T PermuteFromData<T>(this T thisPet, string data) where T : Pet { T newPet = new SomeDeserializer().Deserialize<T>(data); newPet.someProperty = thisPet.someProperty; return newPet; } } This has worked great for me, as I can say things like: Dog newDog = existingDog.PermuteFromData(dogData); Cat

Extension Method ConvertAll

邮差的信 提交于 2019-12-24 03:36:19
问题 What is the proper use of ConverAll ? Will it convert one type to another type? like List<int> intList = new List<int>(); intList.Add(10); intList.Add(20); intList.Add(30); intList.Add(33); var query= intList.ConvertAll(x=>(double)x); for this i can use cast or OfType<>. 回答1: ConvertAll isn't an extension method, it's a real method on List<T> itself. It returns a new list containing the converted elements. So in your example, the query variable isn't actually a query, it's a List<double> .

C# Extension Method Return Value Not Setting Variable

会有一股神秘感。 提交于 2019-12-24 03:16:31
问题 Why doesn't this extension method set the value it's applied to? public static byte ExtSetBits(this byte original, byte value, byte bitSize) { unchecked { original &= (byte)~bitSize; } original |= (byte)(value & bitSize); return original; } This is the call ( selectedIndex = 13 ): byte test = 0xFF; test.ExtSetBits(selectedIndex, 0x1F); Console.WriteLine("test:" + test.ToString("X").PadLeft(2,'0')); Writes "test: FF" to the console. If I do this it works: byte test = 0xFF; test = test

How do I tell linq2db how to translate a given expression, ie Split(char) into SQL when it does not know how to do so?

China☆狼群 提交于 2019-12-24 02:39:15
问题 I am using linq2db and while it works well enough for most CRUD operations I have encountered many expressions that it just cannot translate into SQL. It has gotten to the point where unless I know in advance exactly what kinds of expressions will be involved and have successfully invoked them before, I am worried that any benefit derived from linq2db will be outweighed by the cost of trying to find and then remove (or move away from the server side) the offending expressions. If I knew how

Creating a table method on a user defined type (like like 'nodes' on the XML data type)

北城以北 提交于 2019-12-24 02:21:57
问题 I've created working SQLCLR-based user defined table-valued functions as well as user defined types. What I want now is a method on a SQLCLR UDT that returns a table, similar to the nodes method on the XML data type. The TableDefinition and FillRowMethodName properties of the SqlMethod attribute / decoration seem to imply that it should be possible, but nothing actually works. When I call the method like this (which I expect to fail): SELECT @Instance.AsTable(); I get: invalid data type which