Jagadeesh N
Working on Big data...
Homepage: https://jagadeesh7n.wordpress.com
Reports in MS Office formats using OpenXML
Posted in MS Techs on November 1, 2013
I had a requirement in my current project to generate dynamic reports in various formats like word, excel, power point (Microsoft office formats). Client asked for power point slides with dynamically generated charts embedded in it and the chart data can be edited. When I goggled for this requirement i came across many tools and frameworks which generate chart as an image where the data cannot be edited. Finally I got the solution using OpenXML. We can generate any kind of reports in MS Office format. We can use OpenXML SDK tool to generate code in .net programming languages and later the code can be modularized to cater our requirement easily.
Useful Features in LINQ
Posted in LINQ on November 1, 2011
LINQ Useful Examples
1. PLINQ
Using AsParallel()
Example:
IList<int> sequence = new List<int>();
sequence.AsParallel.Select(x => x);
This will take an advantage of multiple processors.
Internally the data source is partitioned and the query operates on each partition in parallel. In case parallelization is not possible, the query is executed sequentially
2. Compare two sequences in LINQ
Enumerable. SequenceEqual ()
SequenceEqual() compares the source and target sequences elements, by using the default equality comparer for their type, and returns a Boolean.
Example
Var l1 = new[] {1,2,3,4};
Var l2 = new[] {1,2,3,4,5};
Var result = l1.SequenceEqual(l2);
Console.WriteLine(“Are they Equal?: {0}”, result);
The output will be: False
3. Listing Classes from current domain which implements IEnumerable
Var t = typeOf(IEnumerable);
Var enumTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x => t. IsAssignableFrom(x))
.Select(type => type.FullName)
4. OuterJoin in LINQ
Using DefaultIfEmpty() keyword
Example:
Pulbic class Person
{
Public int PersonId {get;set;}
Public stirng PersonName {get;set;}
}
Pulbic class PersonPhoneNumber
{
Public int PersonId {get;set;}
Public stirng PhoneNumber{get;set;}
}
List< Person> persons = new List< Person>();
Persons.Add(new Person()
{
PersonId = 1,
PersonName = “Jack”
});
Persons.Add(new Person()
{
PersonId = 2,
PersonName = “John”
});
List< PersonPhoneNumber> personPhoneNumbers = new List<PersonPhoneNumber>();
personPhoneNumbers.Add(new PersonPhoneNumber()
{
PersonId = 1,
PersonPhoneNumber = “123534”
});
LINQ without OuterJoin
Var result = (from p in persons
Join pn in personPhoneNumbers
on p.PersonId equals pn.PersonId
Select new {
PersonId = p.PersonId,
PersonName= p. PersonName,
PhoneNumber= pn.PhoneNumber
}).ToList();
From above LINQ it will return only on List Item of person id 1
[0]
PersonId = 1,
PersonName= “Jack”,
PhoneNumber= “123534”
If we need to get both the persons result even though personPhoneNumbers collection doest not have data for the PersonId =2
Var result = (from p in persons
Join pn in personPhoneNumbers
on p.PersonId equals pn.PersonId
into allpersons
from ap in allpersons.DefaultIfEmpty()
Select new {
PersonId = p.PersonId,
PersonName= p. PersonName,
PhoneNumber= ap.PhoneNumber
}).ToList();
The result from above query is
[0]
PersonId = 1,
PersonName= “Jack”,
PhoneNumber= “123534”
[1]
PersonId = 2,
PersonName= “John”,
PhoneNumber= Null
5. Using Let Keyword in LINQ
Let keyword used in LINQ to keep some value (maybe single or collection) for each object in from keyword in LINQ and same let value can be used in Select block ofLINQ.
Example:
var arr = new[] { 5, 3, 4, 2, 6, 7 };
var sq = from int num in arr
let square = num * num
where square > 10
select new { num, square };
foreach (var a in sq)
Console.WriteLine(a);
Result of Above Code is
{num = 5, square = 25}
{num = 4, square = 16}
{num = 6, square = 36}
{num = 7, square = 49}
Difference Between Select and SelectMany in LINQ
Posted in LINQ on October 19, 2011
If we have objects with Enumerable of Enumerable objects then SelectMany keywork in LINQ helps to flatten Enumerable<Enumerable<T>> to Enumrable<T>
Lets See with an example
Class-1
Public Class Contact
{
public string PhoneNumber {get;set;}
}
Class-2
public class Person
{
public string PersonName { get; set; }
public IEnumerable<Contact> Contacts {get;set;}
}
Adding list of Persons
IEnumerable<Person> persons = new List<Person>();
IEnumerable<Contact> person1Contacts = new List<Contact>();
Contact contact1 = new Contact()
{
PhoneNumber = “12345567”
};
Contact contact2 = new Contact()
{
PhoneNumber = “9876656”
};
person1Contacts.Add(contact1);
person1Contacts.Add(contact2);
Person person1= new Person()
{
PersonName = “Jack”
Contacts = person1Contacts
};
Persons.Add(person1);
IEnumerable<Contact> person2Contacts = new List<Contact>();
Contact contact3 = new Contact()
{
PhoneNumber = “567234”
};
Contact contact4 = new Contact()
{
PhoneNumber = “06512349”
};
Person2Contacts.Add(contact3);
Person2Contacts.Add(contact4);
Person person2= new Person()
{
PersonName = “John”
Contacts = person2Contacts
};
Persons.Add(person2);
//Now we have two persons objects in Persons collection
—Using Select Keywork in LINQ—-
Persons.Select(x => x.Contacts)
Result
Result from above Linq will be having two collection like below
[0]
12345567
9876656
[1]
567234
06512349
—Using SelectMany Keywork in LINQ—-
Persons.SelectMany(x => x.Contacts)
Result
Result from above Linq will be having only one collection like below
[0]
12345567
9876656
567234
06512349
ZIP operator in LINQ
Posted in LINQ on October 19, 2011
ZIP operator in LINQ indroduced in C# 4.0.This operator can zip together two sequences.
Sample:
Lets consider two sequences
string[] names = {“Jack”,”john”}
int[] ages = {27,35}
names.Zip(ages, (name,age) => name + “is” + age + “years old”)
OutPut:
jack is 27 years old
john is 35 years old
New Features in Entity Framwork 4.1
Posted in Entity Framework on October 13, 2011
ADO.NET Entity Framework 4.1 introduces two new features:
- The DbContext API is a simplified abstraction over ObjectContext and a number of other types that were included in previous releases of the ADO.NET Entity Framework. The DbContext API surface is optimized for common tasks and coding patterns. DbContext can be used with Database First, Model First and Code First development.
- Code First is a new development pattern for the ADO.NET Entity Framework and provides an alternative to the existing Database First and Model First patterns. Code First is focused around defining your model using C#/VB.NET classes, these classes can then be mapped to an existing database or be used to generate a database schema. Additional configuration can be supplied using Data Annotations or via a fluent API.
References :
http://blogs.msdn.com/b/cesardelatorre/archive/2011/04/14/entity-framework-4-1-just-released.aspx
http://msdn.microsoft.com/en-us/library/gg696165(v=VS.103).aspx
JQuery 1.7 Beta1
Posted in JQuery on October 2, 2011
T-SQL New and Enhanced Functions
Posted in SQL Server on October 2, 2011
SQL Server introduced new and enhanced function which really helps to avoid some clumpsy coding with existing functions
read out this blog it is really very helpful.
Adding dropdownlist to telerik mvc grid
Posted in MVC TELERIK on July 22, 2011
If we use AJAX binding for mvc telerik grid binding then to add dropdownlist to telerik grid we should go with ClientTemplate.
find below example where we can bind grid with columns Employee and Department (as a dropdown list with respective department selected for the employee)
Model Class
========
Public class YourModel
{
public string Employee;
public string Department;
}
Razor View
=========
@(Html.Telerik().Grid<YourModel>() .Name("gridWithDropDown") .Columns(columns => { columns.Bound(m => m.Employee); columns.Bound(m => m.Department); .ClientTemplate( Html.DropDownListFor(m => m.Department,Model.Departments).ToHtmlString() ) } .DataBinding(databinding => { databinding.Ajax().Select("your Action name","Controller Name") }) .ClientEvents(events => { events.OnRowDataBound("OnRowDataBound") }) Javascript ============== <script type="text/javascript"> function OnRowDataBound(e) { $(e.row).find('#Department').val(e.dataItem['Department']); } </script>