ADO.NET :: How To Pass A KeySelector Expression To An Extension Method
Dec 21, 2010
I am stuck with the database row level security model that my predecessor has implemented on this project. I am trying to make an extension method for my linq to sql classes to make it easier to filter rows of data in my linq queries. The problem I am facing is that I need to somehow tell it how to access the primary key value of the object in the where clause. I get errors about DynamicInvoke(object[]) cant be converted to SQL. Here is a sample of what I am working with:
I am using Tuple class in WCF Service. When i add reference to my web project I get Tuple class. Now i want to add an extension method to Tuple<> class in my service which will be accessible in my Web project.
I recently found out about C# extension methods and wrote this one:
/// <summary> /// Short-hand for setting the data source and binding it. /// </summary> /// <param name="me">The control to set and bind the data source of.</param> /// <param name="dataSource">The data source.</param> public static void BindTo(this DataBoundControl me, IEnumerable dataSource) { me.DataSource = dataSource; me.DataBind(); }
What do you guys think? Is this a reasonable extension method to use in a professional ASP.NET project?
Imports System.Runtime.CompilerServices Namespace Extensions Public Module IntegerExtensions <Extension()> Public Function ToCommaDeliminatedNumber(ByVal int As Integer) As String Dim _input As String = int.ToString Select Case int Case Is > 99999 : Return _input.Remove(_input.Length - 3) & "k" Case Is > 9999 : Return Math.Round(Double.Parse(int / 1000), 1).ToString & "k" Case Is > 999 : Return String.Format("{0:N0}", int) Case Else : Return _input End Select End Function End Module End Namespace
And in one of my classes I'm using user.Reputation.ToCommaDeliminatedNumber I am importing the Extensions Namespace into the Class, but the error I'm getting is...'ToCommaDeliminatedNumber' is not a member of 'Integer?'. I do have other Extension Methods for Strings and Dates that work exactly as expected... I'm just at a loss on this one.
I know there is a ListBoxFor extension method among the ASP.NET MVC Html helper extension methods, but I always thought that a checkbox list is more user-friendly than a list box.
There was a very convenient CheckBoxList control in good old WebForms, but obviously that is out of the picture now. The question is, why is there no way in ASP.NET MVC to create a check box list? How can I write my own extension method that creates a check box list and behaves in a similar way ListBoxFor behaves?
I want my ASP.NET HTTP handler to be invoked when a user opens an URL of this format:http://mySite/getStuff?id="actualId"In order to do that on IIS5.1 I had to open the IIS management console and map .* extension onto ASP.NET ISAPI handler so that the request is processed using ASP.NET in the first place.Turns out IIS6 console won't accept .* as a valid extension.I know there's such thing as URL rewrite, but I feel it's an overkill for such simple case.Are there some easier ways to pass all requests to URLs that don't have an extension to ASP.NET?
I am using the Directory.GetFiles method as follows:
[Code]....
As expected, it returns files with the ".pdf" extension. I would like to be able to specify more than one extension (like "*.doc*" and "*.txt*", etc.). How can I use GetFiles to return files with more than one desired extension, without returning all files?
I'm binding an object to a gridview. The object (LeClient) consists of several variables, two of which are related to its phone number. One variable contains a string of digits (LePhone) and the other contains an int that represents the country code (LeCountryCode). I have an extension method for strings that works to format the string LePhone and that I'd like to pass it LeCountryCode as the parameter.
So far, on RowDataBound I have an event handler with the following line:
I'd like to replace the 1 with the corresponding country code that's stored in the object LeClient associated with the row. How does this work? I tried
I would like to know what the pros and cons are of creating an extension method v a partial view. From my understanding, htmlHelper extension methods are better as they enable one to unit test the view.
e.g. Assert.AreEqual(Html.Price(), "<td><tr><td>Price</td><td>4.50</td></tr>");
where the Price() method contains the presentation logic.
Since both the extension method and partial view are reusable, what then is the point / benefit of a partial view?
I have an ObjectDataSource that I want to perform updates using a business entity i.e. Type="Object"). Since the values for the entity are within a user control, I have stored a reference to the control in Session and in the updating event, set the new instance to the value of the entity from the user contol property (which also pulls values from the form viaother properties of the control):
Protected Sub MasterDataSource_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles MasterDataSource.Updating Dim entity As New Login() Dim accountControl As AccountInfo = TryCast(Session("AccountCtrl"), AccountInfo) entity = accountControl.Entity e.InputParameters.Add("entity", entity) End Sub
My question is, how can I get the update method to pass this entity to the update method in my BLL class? It seems the Update method requires an ID or reference to the original object to use in determining whether any changes have taken place, but I don't really want to do this. In other words, I just want to use the Update event on my ObjectDataSource to pass my entity to the method ("Update") I set as a property and then let this business method handle the update of the data. Shown below, is the BLL update method I want to call:
Public Overloads Function Update(ByVal entity As Login) If entity Is Nothing Then Throw New ArgumentNullException("entity") End If MyBase.Update("UpdateLogin", entity.Username, entity.Password, entity.FirstName, entity.LastName, entity.Role, entity.Region, _ entity.Email, entity.Title, entity.TierID, entity.Street, entity.City, entity.State, entity.Zip, entity.Mobile, entity.Phone, entity.Fax) End Function
When I try to call this as it stands now, I get an error: ObjectDataSource 'MasterDataSource' could not find a non-generic method 'Update' that has parameters: ID, entity. Previously, I'd set up a long list of parameters of basic data types (string, int, boolean), but this is rather cumbersome and I was hoping to use an entity for this (FYI, I also got the same type of error when I tried this approach, but with the ID as the last parameter in the list). Perhaps what I'm doing here is atypical to how the ODS is normally used?? Has anyone done something like this successfully?
I have an extension method which I can use from the .cs codebehind of an aspx page, but if I try to do it in a code block in the aspx, it can't find the extension method. Is there something I need to add to the page?
Inside my repeater I'm calling an extension method like so:
[Code]....
he above is giving me an "Object not set to an instance of a object" exception inside the extension method. If I comment the above expression out, it works fine, and the items are binded wtih no problems.
I am using MultiView and View in .NET but i am getting this error: Compiler Error Message: CS1061: 'ASP.default_aspx' does not contain a definition for 'NextView' and no extension method 'NextView' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)Source Error:
Line 24: THIS IS VIEW 1 . HAVE A LOOK ! iTS COOL BECAUSE OF C#.<br /> Line 25: <br /> Line 26: <asp:Button ID="Button1" runat="server" Text="nEXT" OnClick="NextView" /> Line 27: <br /> Line 28: <br /> Source File: c:UsersxxxxDocumentsVisual Studio 2012ProjectsWindowsAzure2TestCRoleDefault.aspx Line: 26
This is my aspx.cs code: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System. Web.UI. WebControls;
public static T Sum<T>(this DataTable dt, string columnName) where T : IEnumerable<decimal>, IComparable<decimal>[code]....
It feels like i'm almost there, but not quite :/
Just trying to sum up either decimal or int values in a column in datatable. Currently getting a compile error, however think this is due to the incorrect generic constraint.
I have a method that lists all Properties from an expression.
It goes like this:
[Code]....
After that, I would put it to string (i.e. "Property1.Property2.Property3"). What I want to do is if a Call expression type comes up - only for indexed properties - , it should have some way of adding it so when I put it to string it would output "Property1.Property2[0].Property3".
how the Lambda expression work specially in case of parameters (in the above case a & b) ? What is the basic concept of parameters in Lambda expression ? Can we use the names of the variables defined outside of the query instead of a & b ? which elements a & b represent in the above case represent ? Can we pass parameters from outside in the query ? If we can pass parameters from outside query then how it will be done? If you give me any other example instead of the above query to understand the concept of parameter in Lambda expression.
then i can show this dropdownlist in my "about" view <%=Html.DropDownList("a") %>.
BUT, now i want to define this dropdown list at my back end. what i want to do is to create a new "Helper".cs file in my project, and create a function within it. so every time if i want to show this dropdown list, i can just simply call this function instead of Add new SelectListItems one by one. is there anyone tell me how to do it?? and is it a good way to create a dropdownlist at the back end?