Friday, September 20, 2013

Customised Debugger Output using DebuggerDisplay Attributes

 

Debugger display attribute helps to customized debugger message which in turn create more meaningful debug messages.

When we try debugging the below sample code

 var order = new Order
                {
                    OrderId = Guid.NewGuid(),
                    OrderDate = DateTime.Now,
                    Amount = 100.00M,
                    Customer = new Customer()
                        {
                            Id = Guid.NewGuid(),
                            Name = "Arjit"
                        }
                };

 

 


image

when debugged the sample code above and hover the order variable we can see the name of the Type the order variable is holding. When implementing the debugger attributes the same code is seen like below in debug mode

image

Lets see How to implement this For creating custom debug messages we need to decorate class with DebuggerDisplay.
 [DebuggerDisplay("New Order is Created By {Customer.Name} on 
{OrderDate.ToShortDateString()} For ${Amount}")]
class Order
{
public Guid OrderId { get; set; }
public DateTime OrderDate { get; set; }
public decimal Amount { get; set; }
public Customer Customer { get; set; }
}
[DebuggerDisplay("New customer is {Name}")]
class Customer
{
public Guid Id { get; set; }
public string Name { get; set; }
}
If You don’t want the properties to be appearing on debug window the use
 
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Guid Id { get; set; }

Tuesday, August 13, 2013

Row Constructor - Multiple Row Inserts Using Single SQL Query

Row constructor feature of SQL Server 2008 help with single query to insert multiple rows

Old Approach
 

insert into Customer(id,name,Age) values (100,'Ricardo',45)
insert into Customer(id,name,Age) values (101,'Michael',42)
insert into Customer(id,name,Age) values (102,'Antony',44)
insert into Customer(id,name,Age) values (103,'Zeus',43)




New Approach
 
 
insert into 
 Customer(id,name,Age)
 values
   (100,'Ricardo',45),
   (101,'Michael',42),
   (102,'Antony',46),
   (101,'Zeus',43)


Tuesday, August 06, 2013

Distinct() LINQ

Distinct() in LINQ is usually used to return Distinct item based on default comparison logic.
  
string[] customer = { "Jeniffer", "Steve", "Anderson", "Evan" };
var distinctCustomerList = from customerList in
customer.Distinct()
select customerList;








 

We need to tell Distinct to ignore the case by following.

 string[] customer = { "Jeniffer", "Steve", "Anderson", "Evan", "EVAN" };

var distinctCustomerList = from customerList in
     customer.Distinct(StringComparer.CurrentCultureIgnoreCase)
     select customerList;



This is pretty straight forward and LINQ uses its default Compare logic to find Distinct.
The default Compare Logic need to be changed when working on custom objects like below

 
public class Customer
{
public string CustomerId { get; set; }
public string FirstName { get; set; }
}

We need to implement IEquatable interface for determining equality of instances.

  
public class Customer : IEquatable
{
public string CustomerId { get; set; }
public string FirstName { get; set; }

public bool Equals(Customer other)
{
//Check whether the compared object is null.
if (Object.ReferenceEquals(other, null)) return false;

//Check whether the compared object references the same data.
if (Object.ReferenceEquals(this, other)) return true;

//Check whether the Customer' properties are equal.
return CustomerId.Equals(other.CustomerId) && FirstName.Equals(other.FirstName);
}

public override int GetHashCode()
{

// Get the hash code for the Textual field if it is not null.
int hashTextual = CustomerId == null ? 0 : CustomerId.GetHashCode();

// Get the hash code for the Digital field.
int hashDigital = FirstName.GetHashCode();

// Calculate the hash code for the object.
return hashDigital ^ hashTextual;
}
}



Finally call the Distinct methods to filter out duplicates

  
var cList = new List();
cList.Add(new Customer { CustomerId = "100", FirstName = "Jeniffer" });
cList.Add(new Customer { CustomerId = "100", FirstName = "Steve" });
cList.Add(new Customer { CustomerId = "102", FirstName = "Anderson" });
cList.Add(new Customer { CustomerId = "103", FirstName = "Evan" });
cList.Add(new Customer { CustomerId = "103", FirstName = "Evan" });
IEnumerable lstCustomer = cList.Distinct();

Wednesday, June 06, 2012

People search engine Pipl's

API can be used to bring a variety of information about an individual, from social networks like Facebook and LinkedIn to government resources like the United States Patent and Trademark Office and county clerk offices, into any application

development,search

Thursday, May 31, 2012

My first FireFox plugin released

Here are few screen shots of this

clip_image002clip_image002[4]

clip_image001clip_image002[6]

imageimage

Wednesday, May 30, 2012

Cast<DataRow>

the following code will load top 5 rows Order in ascending row filter rows based on current date 

   1: return rows.Count() > 0 ? dt.Rows.Cast<DataRow>()
   2: .OrderByDescending(r => r["StartDate"]).
   3: Where(j => (DateTime)j["StartDate"] >= DateTime.Now).
   4: Take(5).CopyToDataTable() ;

Discover who’s tracking you online

Collusion is an experimental add-on for Firefox and allows you to see all the third parties that are tracking your movements across the Web. It will show, in real time, how that data creates a spider-web of interaction between companies and other trackers

more info

Wednesday, April 20, 2011

linqpad

 

LINQPad is a free software utility for Microsoft .NET developers using LINQ. It allows developers to interactively query SQL databases and other data sources such WCF Data Services using LINQ.

LINQPad supports the following LINQ dialects:

  • LINQ to Objects
  • LINQ to SQL
  • Entity Framework
  • LINQ to XML


It also supports writing queries in SQL as well.

Aside from being useful for writing queries, LINQPad can be used to interactively write C# code without having to compile it. This expands its use to a general "test workbench" where C# code can be quickly prototyped outside of Visual Studio.

download linqpad