How FirstOrDefault function works?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Selun
    New Member
    • Jan 2011
    • 6

    How FirstOrDefault function works?

    Hi all,
    Im new in coding and i need to change something a code that i barely understand. And the problem is a FirstOrDefalut function.
    Below is the code.
    Code:
    public static string GetArticlePageUrl(Article article, int categoryId)
    	{
    		
    		if (categoryId < 0)
    		{
                ArticleCategoryLink acl = article.ArticleCategoryLinks.FirstOrDefault();
    
    			if (acl != null && acl.Category != null)
    				categoryId = acl.Category.ID;
    		}
    
    		if (categoryId > 0)
    			retVal = retVal + string.Format("&cid={0}", categoryId);
    
    		return retVal;
    	}
    I just want to change
    Code:
    ArticleCategoryLink acl = article.ArticleCategoryLinks.FirstOrDefault();
    whitch now get the first or default categoryid among the categories that the article belongs, to get the second value, or the first among some certain values that i can give, or the first among all excluding some values that i ll give.

    Im sorry if its Code is messy, or if i missed to give any info. i just dont know what i should post to help you giving me an answer.

    Thank you in advance.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You haven't explained your question very well. Give it another try. Also, how can we help if we cannot see the method-in-question's code? Post the code for FirstOrDefault( ).

    Comment

    Working...