views:

246

answers:

1
select new FeedResource
{ 
   Title = (string)details.Element("title"),  
   Host = (string)details.Element("link"),   
   Description = (string)details.Element("description"), 
   PublishedOn = (DateTime?)details.Element("pubDate"), 
   Generator = (string)details.Element("generator"),  
  Language = (string)details.Element("language")
}

In the above code i want to pass the type cast value to another function, Example

Description = getValidDescription((string) details.Element("description"))

but i am not able to achieve, any inputs?

Note : The type casting is required to handle null values (that is in case no value present for "description" it (XElement) handles null perfectly.

A: 

Working fine for me; what is the error message? Example:

// doesn't have to be static - just simpler for my test
static string getValidDescription(string description)
{
    // handle nulls safely (could return a default here)
    if (description == null) return null;
    // for example only...
    return CultureInfo.CurrentCulture.TextInfo
        .ToTitleCase(description);
}

var qry =
    from details in doc.Root.Elements("detail")
    select new FeedResource
    {
        Title = (string)details.Element("title"),
        Host = (string)details.Element("link"),
        Description = getValidDescription((string) details.Element("description")),
        PublishedOn = (DateTime?)details.Element("pubDate"),
        Generator = (string)details.Element("generator"),
        Language = (string)details.Element("language")
    };
Marc Gravell