Feeds:
Posts
Comments

Archive for the ‘C Sharp’ Category

List<Nullable<int>> custIDs = (from l in lookUpList 
                               group l by l.CustID into g
                               select g.Key).ToList<Nullable<int>>();
var customers=from c in new Repository().GetCustomers()
              where custIDs.Contains(c.CustID)
              select c).ToList();

Read Full Post »

List<Nullable<int>> custIDs = (from l in lookUpList                                          
                              select l.CustID).ToList<Nullable<int>>();
 var customers =from c in new Repository().GetCustomers()                                                
                where custIDs.Contains(c.CustID)                                                
                select c).ToList();

Read Full Post »

A simple code snippet to demonstrate String to DateTime and DateTime to String conversions

     DateTime dt = DateTime.ParseExact("2010-05-21", "yyyy-MM-dd", null);

     Console.WriteLine(dt.ToLongDateString()); // Display the long date string
     Console.WriteLine(dt.ToString()); // Display the default date string
     Console.WriteLine(dt.ToShortDateString()); // Display the short date string
     Console.ReadLine();

Source : http://www.codeproject.com/KB/cs/String2DateTime.aspx

Read Full Post »

            string path = @"c:\samplefile.txt";
            StreamReader streamReader = new StreamReader(path);
            string line=streamReader.ReadLine();
            while ( line!= null)
            {
                Console.WriteLine(line);
                line = streamReader.ReadLine();
            }
            Console.ReadLine();

Read Full Post »

Loading XML Document from a file.

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\samplexmlfile.xml");

Loading XML Document from string

string fileContent ="<Root><SomeNode>SomeValue</SomeNode></Root>";
XmlDocument doc = new XmlDocument();
TextReader rdr = new StringReader(fileContent);
doc.Load(rdr);

Hope this help!

Read Full Post »

A neat language syntax in C#

isTrue ? thenDoThis : ElseDoThis

bool isBodyHTML=onFileReceived.Attributes["isBodyHTML"].Equals("true")?true:false

Read Full Post »

C# 4.0 now supports using optional parameters with methods, constructors, and indexers (note: VB has supported optional parameters for awhile).

This is how you declare your method to have optional parameter

public Boolean SendMail( string[] toAddresses, string subject, string body,string sender = "bala.singam@hotmail.com",bool isBodyHTML=false)
{
 
try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From =new MailAddress(sender);
                mailMessage.IsBodyHtml = isBodyHTML;

                foreach (string address in toAddresses)
                    mailMessage.To.Add(new MailAddress(address));

                mailMessage.Subject = subject;
                mailMessage.Body = body;

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Send(mailMessage);

                return true;
            }
            catch (SmtpException ex)
            {
               Console.WriteLine(ex);
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
}

And this is how you call your method with optional parameters/named arguments

//I did't passed sender parameter value
mailer.SendMail(toAddresses:new string[] { "bala.singam@hotmail.com" },subject: "File Received", body:Encoding.UTF8.GetString(msg),isBodyHTML:true);

Source : http://weblogs.asp.net/scottgu/archive/2010/04/02/optional-parameters-and-named-arguments-in-c-4-and-a-cool-scenario-w-asp-net-mvc-2.aspx

Read Full Post »

Older Posts »

%d bloggers like this: