0

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();
}
1

3 Answers 3

1

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Actually, it returns default(T) if there are no results. It will return null in this particular case.
thanks..I have 3 typenames ( CUSTOM,STANDARD,NORMAL) can you please advise the code how to use this method and store values in a dropdownlist
@Raman that depends on what kind of application you're developing and what framework youre using. Please open a separate question and show what you have so far.
@Raman: But be cautious... Attempting to build an application piece by piece by asking questions on Stack Overflow isn't going to work. Pick your battles; work for awhile until you get stuck, and then ask a question here. Read MSDN or a book if you need detailed background information to complete your app.
0

FirstOrDefault return first element in sequence, in this sample ApplicationTypes is your sequence or a default value if the sequence contains no elements.

1 Comment

Thanks Candie...one more thing how can i return var value like in above example I want to return Typenames
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.