• Home
  • About
  • BestBloggingIdeas
  • DotNetLearningSource
  • FORUM
  • Joblinks
  • Latest News
  • Policy
  • POSTS
  • SimplySqlServer.Com && SimplyAspDotNet.Com
  • Sitemap

Join Ours Forum

Asp.Net,C#,Ajax,Sql server,silverlight,Javascript codes exambles articles,Programming exambles

RSS Feed
  • Bounty Huge Roll [Amazon Frustration-Free Packaging]
  • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
  • The Basic Overview of Windows Mobile Development Asp.Net C#
  • Overview of Sql server extended properties Asp.Net C#
  • How to Use Sql Server Extended properties using visual studio Asp.Net C#
  • Adobe Dreamweaver Templates Accelerate Web Development
  • Top Tips for Web Design Projects
  • How to Achieve a Good Web Design Structure
  • To Use Or Not To Use Website Templates
  • Five Tips to a Successful Website
  • Top 10 Articles,


    Silverlight Datagrid Select Update Delete Insert Asp.Net C#

    Differences Similarities Benefits Between Typed Datasets and Untyped Datasets asp.net c#

    Linq to Sql Introduction Entities Ado.Net C# SqlClasses Attributes Linq Mapping

    Linq Programming/How Linq Works?/Linq Implementation In Asp.Net C# Ado.Net

    Performing Developing Using Investigating Asp.Net 2.0 Ajax Application Development Asp.Net C#

    Hosting/Install Wcf Services in a Windows Service Asp.Net C#

    Connecting Silverlight to Wcf Asp.Net C#

    Silverlight Data Grid Data Binding WCF Asp.Net C#

    Invoking/Accessing/Calling WCF Service Without Adding/Creating Proxy/Reference Asp.Net C#

    Performing Doing Creating Insert Update Delete sql data Using Linq Database Asp.Net C#

    concatenating delimited strings from object properties Asp.Net C#

    Posted by james on April 28, 2011 Leave a comment (0) Go to comments

    The sample code we’ve looked at so far has been reasonably simple. Let’s look at a more difficult problem—building a delimited list composed of values that are calculated from object properties.

    Suppose we have a simple class named Party:

    PartyDemo.cs (excerpt)

    public class Party

    {

    public Party(DateTime partyDate)

    {

    this.partyDate = partyDate;

    }

    public DateTime PartyDate

    {

    get

    {

    return partyDate;

    }

    }

    DateTime partyDate;

    }

    Consider the scenario that we need to concatenate instances of this class together. The desired output is a pipe-delimited list of the number of days between now and the SomeDate value.

    Solution

    Our first step is to determine where to place the logic that will perform the calculation and concatenation. The best place for this logic is in a predicate method that’s called from the collection’s Join method.

    Performing predicate-based operations on your generic collections can really simplify and enhance your code.

    The following example, in which we concatenate our Party objects, is not only useful on its own—it should also help to demonstrate the thought process behind moving from loop-based logic to clean, simple code that leverages predicates.

    Let’s start by defining a new Join method that can take in a delimiter, an enumeration, and an instance of the converter delegate. The converter delegate has the following signature:

    delegate TOutput Converter (TIn input)

    As an argument to the Join method, we specify that TOutput should be a String, leaving the input as a generic object:

    PartyDemo.cs (excerpt)

    public static string Join(string delimiter

    , IEnumerable items

    , Converter converter)

    {

    StringBuilder builder = new StringBuilder();

    foreach(T item in items)

    {

    builder.Append(converter(item));

    builder.Append(delimiter);

    }

    if (builder.Length > 0)

    {

    builder.Length = builder.Length – delimiter.Length;

    }

    return builder.ToString();

    }

    With this method defined, we can concatenate an Array or collection of Party instances, like so:

    PartyDemo.cs (excerpt)

    Party[] parties = new Party[]

    {

    new Party(DateTime.Parse(“1/23/2006″))

    , new Party(DateTime.Parse(“12/25/2005″))

    , new Party(DateTime.Parse(“5/25/2004″))

    };

    string result = Join(‘|’, parties

    , delegate(Party item)

    {

    TimeSpan ts = DateTime.Parse(“11/24/2006″) – item.PartyDate;

    return ((int)ts.TotalDays).ToString();

    });

    Console.WriteLine(result);

    Note that we make use of an anonymous delegate that examines an instance of Party and calculates the number of days that have passed since PartyDate. This calculation returns a string that will be concatenated to the previous item in the list.

    That code produces the following output: 305|334|913.

    Discussion

    Here’s what you’ll gain by moving from “dumb” loops to predicate-based operations:

    *

    Your code will be more reusable, since you can reuse generic methods with different objects. A good example of this is the ActiveRecord implementation in SubSonic, an open source Data Access Layer that we’ll be exploring in detail in 17 “Advanced Topics”.

    *

    Your code will leverage framework methods rather than require you to write your own repetitive code. Less custom code means fewer custom bugs.

    *

    Your library code will be likely to perform better, since it will be strongly typed.

    Tip: Where to Sort and Filter

    In this solution, and those prior to it, we’ve looked at ways to sort and filter objects in the application layer. But don’t take this to mean that you should necessarily handle tasks like this in your application code rather than in the database. Generally, databases will be more efficient when it comes to sorting and filtering, although complex operations like string manipulation are better handled by application code.

    ASP.NETAspNet, concatenating, delimited, Object, properties, Strings
    ← How to Use Open Source Project in asp.net project c#
    Converting Generic lists into Specific classes asp.net c# →

    Learn Easily Using Video Tutorials


    How to choose the right Java IDE – explained Eclipse NetBeans BlueJ

    Developing/Creating/Performing/Configuring Java Applications Using Eclipse IDE

    Step By Step Guide for Download/Install Configure Eclipse IDE for Java

    Editing data with the GridView control Asp.Net C#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Best way to prepare asp.net Interview - Success Stories

    Download Important Questions and PPT's:

    Sql Server Important Questions Online free download

    Dotnet Important Questions Online free download

    Exploring Linq to Sql Process Flow

    Learn how to perform silverlight programming

    Learn OOPs concepts in better and well manner

    Learn Ajax in better and well manner

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    *

    *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Enter your email address:

    Delivered by FeedBurner

    • Recent Posts

      • Bounty Huge Roll [Amazon Frustration-Free Packaging]
      • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
      • The Basic Overview of Windows Mobile Development Asp.Net C#
      • Overview of Sql server extended properties Asp.Net C#
      • How to Use Sql Server Extended properties using visual studio Asp.Net C#
    • Search by Tags!

      Application AspNet Basic between Black Bluetooth Build Business Collection Consultants Design Development Downloading effective Excel Experts Generics Implement Installing Interview Logic Management Microsoft Minutes Object Outlook Professional Programmer Programming Project Projects Questions Ready Select Server Services Silverlight Source Strings Studio Through using Visual Website Wordpress
    • Archives

      • August 2011
      • June 2011
      • May 2011
      • April 2011
      • March 2011
      • February 2011
      • December 2010
      • November 2010
      • October 2010
      • September 2010
      • August 2010
      • July 2010
      • June 2010
      • May 2010
      • April 2010
      • March 2010
      • February 2010
      • January 2010
      • December 2009
      • November 2009
      • October 2009
      • September 2009

    Copyright © 2012 NetProgrammingHelp.com

    Δ Top