Wednesday, April 11, 2012

How to Order by Multiple in Linq

When Ordering custom collection in Linq  using multiple fields you can use OrderBy and ThenBy methods.

Example

private BOMLineDTO[] SortBOMLines(BOMLineDTO[] bomLines)
{
BOMLineDTO[] sortedBOMLines = null;

if (null != bomLines)
{
sortedBOMLines = bomLines.OrderBy(record => record.ItemDescription, new BOMLineComparer<StringField>()).
ThenBy(record => record.BOMCategoryType, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTColor, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTSize, new BOMLineComparer<BasicDataField>()).ToArray();
}
return sortedBOMLines;
}
Here BOMLineComparer<T> is My own class which is derived form IComparer<T> interface


Related Posts:http://prabodha-eranga.blogspot.com/2012/05/linq-orderby-custom-comparer.html

Tuesday, March 27, 2012

Return empty IEnumerable<T> in C sharp

In order to return empty IEnumerable  in csharp you can use static Empty<T>() method in Enumerable class.

Suppose i have custom class called Foo  then,

Enumerable.Empty<Foo>();

Thursday, March 22, 2012

The workflow could not update the item, possibly because one or more columns for the item require a different type of information

I have SPD work flow which associated with a list that derived from content type. Using my .net application which was written using SharePoint Client Object Model i update .list item.But in some instances my workflow corrupted  by mentioning  above error.As well as my workflow has configured to initiate when list item creation only.

Finally i figure out reason for this strange behavior

There is one instance in my application in which i create list item and then updates same item to particular state.Due to these two step happen in instantly my workflow getting failed.

Solution

As solution what i have done is to make sure properly workflow initiated or not by querying the workflow instance id.Once i confirmed it then i execute rest of the code.other wise my operation is sleep till workflow initiated.From the beginning of my work flow i saved workflow instance id as list item.

As follows you can get the workflow instance id,

public string GetWorkflowURL(string styleNo, int versionId)
{
/*Getting the update list item */
ListItem listItem = GetUpdateItem(styleNo, versionId);
ListColumns listColumns = new ListColumns();

return listItem != null
? (listItem[listColumns.WorkFlow] != null
? ((FieldUrlValue) (listItem[listColumns.WorkFlow])).Url
: string.Empty)
: string.Empty;
}

This work around has worked to me.

Monday, March 19, 2012

What is Func<T> and Action<T> ?

These two are inbuilt delegate types which can use without defining a new delegate type with each and every parameter type and return type.

Func(Of TResult)

Which can hold method references  which have return types with input parameters or not.As well as there are bunch of overloads
example-:Here I need to add two integers and return the result.. 
Here my Adding method ,which expects the Func with two inputs and one out put.
public static void PerformAction(Func<int,int,int> method,int x,int y)
{
   int result=method.Invoke(x, y);
}
Here my is my controller class
static void Main(string[] args)
{
   Func<int, int, int> addingFunc = (x, y) => (x + y);
   PerformAction(addingFunc , 5, 10);
}

Action(Of T)


Which can hold method references which have no return types.There are bunch of overloads for the Action delegate,
here i use same example without retuning the result,instead of that i write it in to a just Console.
public static void PerformAction(Action<int,int> method,int x,int y)
{
   method.Invoke(x, y);
}
Here my is my controller class
static void Main(string[] args)
{
   Action<int, int> addingAction = (x, y) => Console.WriteLine(x + y);
   PerformAction(addingAction, 5, 10);
}      

Friday, March 16, 2012

Method Overloading error in WCF

In normal behavior of  WCF if you try to implement method overloading in WCF service contract it will throw the an InvaildOperationException once you invoke the service.

[OperationContract]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(EBasicDataType genericType, string descriptionPart);

[OperationContract]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(string styleNo, int versionId, EBasicDataType genericType, string descriptionPart);
In generally above statement will throw the InvaildOperationException.To overcome this problem you have to put OperationContractAttribute with Name Property.
[OperationContract(Name = "GetGenericData")]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(EBasicDataType genericType, string descriptionPart);

[OperationContract(Name = "GetAllgenericData")]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(string styleNo, int versionId, EBasicDataType genericType, string descriptionPart);
In Object Oriented aspect above code is correct ,WCF always generate proxy using the WSDL.The problem is WSDL does not support the concepts like inheritance and overloading.

Tuesday, February 21, 2012

Send Ping Command in C#

I had a scenario which need to test  whether server available or not for my application.So i wrote following code to accomplish that.

.net framework has set of classes which support to Ping to the specified host.

public bool IsAvailableHost(string hostName)
{
var ping = new Ping();
try
{
PingReply reply = ping.Send(hostName);
return reply != null && reply.Status == IPStatus.Success;
}
catch (Exception ex)
{
throw ex;
}
}

Set Alternative Background Color for Wpf ComboBox

 

Using style we can set the alternative background color of a combo box without any code behind.first we have to define the style that targets the combo box item.i have defined this style under the resources section of window

 <Style x:Key="alternativeStyle" TargetType="{x:Type ComboBoxItem}">
     <Style.Triggers>
    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
              <Setter Property="Background" Value="Aqua"></Setter>
         </Trigger>
         <Trigger Property="ItemsControl.AlternationIndex" Value="1">
   <Setter Property="Background" Value="Red"></Setter>
         </Trigger>
   </Style.Triggers>
</Style>



*Note-:You have to defined AlternativeIndex as attached property(ItemsControl Class) Other wise Xaml parser doesn't recognize the AlternationIndex property


Then bind relevant style to the our combo box using Item Container Style

 <ComboBox ItemTemplate="{StaticResource ResourceKey=ListViewModel}" Margin="148,116,180,159" Name="cmbTest" IsDropDownOpen="True" AlternationCount="2" ItemContainerStyle="{StaticResource ResourceKey= alternativeStyle}" />



Finally it shows as…


alternative