Reputation: 35
I have following method. I need to return var tynames by method so what would be the return type of the method will it be List<string>
or something else and also what is the use of FirstOrDefault()
.
Thanks in advance for your reply
public static List<string> AppType()
{
var context = new Dll_IssueTracking.IssuTrackingEntities();// Object context defined in Dll_IssuTracking DLL
var query = from c in context.ApplicationTypes//Query to find TypeNames
select new { c.TypeName };
var **TypeNames** = query.FirstOrDefault();
}
Upvotes: 0
Views: 115
Reputation: 18530
FirstOrDefault
returns the first element found, or the default value (which is null in this case) if the query returned no results.
In this case the return value of the method should be ApplicationType
:
public static ApplicationType AppType()
{
var context = new Dll_IssueTracking.IssuTrackingEntities(); // Object context defined in Dll_IssuTracking DLL
var query = from c in context.ApplicationTypes //Query to find TypeNames
select new { c.TypeName };
return query.FirstOrDefault();
}
Upvotes: 1
Reputation: 31077
FirstOrDefault
is an extension method which looks something like this:
public T FirstOrDefault>T>(this IEnumerable<T> query)
{
return query.Any() ? query.First() : default(T);
}
So, it returns the first element in the sequence if it is not empty, or the default of the type if the sequence is empty.
For Instance, if you have an Enumerable<LinqEntity>
, then most likely default(LinqEntity)
is null
. If you had something like Enumerable<int>
then default(int)
is 0.
Upvotes: 0
Reputation: 28970
FirstOrDefault
return first element in sequence, in this sample ApplicationTypes is your sequence or a default value if the sequence contains no elements.
Upvotes: 0