In this article we are going to see a Extension method which will use to process a user specified default value if value not exist or met the condition.This kind of default should be there not only for FirstorDefault method and also for another Linq methods can be applicable.
Actually when this idea is came to my mind i gave this as suggestion to the Microsoft to add a overloads for some of them Linq Methods
Suggestion posted in Microsoft as :
Microsoft :
Let we see a sample program how to use that extension method
Implementation :
Default
From this article you can learn how to process a user specified default value for FirstorDefault Method if value not exists
Actually when this idea is came to my mind i gave this as suggestion to the Microsoft to add a overloads for some of them Linq Methods
Suggestion posted in Microsoft as :
While select a Value from Collection using Linq , Using Following Methods First,Last Have Default,In that It is already returning the Default value for that Type if the value not exists in collections, Instead of that return Specify a Default of user specified value in the function parameter of the same type. for Example
List<string> coll=new List<string>();
var value = coll.FirstOrDefault(); // Returns null value if not exists in collection as default value
Instead of doing like this specify the user defined default value of same type
var Value=coll.FirstOrDefault("No"); // if default value specifies return that value if no value presents in collection , If default value not specifies then return default of that type .
List<string> coll=new List<string>();
var value = coll.FirstOrDefault(); // Returns null value if not exists in collection as default value
Instead of doing like this specify the user defined default value of same type
var Value=coll.FirstOrDefault("No"); // if default value specifies return that value if no value presents in collection , If default value not specifies then return default of that type .
Posted by Microsoft on 2/16/2014 at 2:00 AM
Let we see a sample program how to use that extension method
internal static class MyLinqExtensions
{
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource>
source, TSource defaultValue)
{
if (source == null) throw new ArgumentNullException("source");
IList<TSource> list = source as IList<TSource>;
if (list != null)
{
if (list.Count > 0) return list[0];
}
else
{
using (IEnumerator<TSource> e = source.GetEnumerator())
{
if (e.MoveNext()) return e.Current;
}
}
return defaultValue;
}
}
Implementation :
class Employee
{
public string Name { set; get; }
public int Id { set; get; }
public string Address { set; get; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> emp = new List<Employee>();
emp.Add(new Employee(){Address = "Chennai",Id
=1,Name = "Rajesh"});
emp.Add(new Employee() { Address = "Madurai", Id =
2, Name = "Siva" });
emp.Add(new Employee() { Address = "Coimbatore", Id =
3, Name = "Suresh" });
emp.Add(new Employee() { Address = "Chennai", Id =
4, Name = "Siva" });
Employee e =emp.Where(x => x.Name == "Rajesh1").FirstOrDefault(new Employee(){Id = 0,Address = "Default",Name = "Default"});
Console.WriteLine(e.Name);
Console.Read();
}
}
In the above example you can see the there is no name Rajesh1 so the linq query normally return null as default but because of our extension method we can pass a user defined value as default.
Output:
Default
From this article you can learn how to process a user specified default value for FirstorDefault Method if value not exists