Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,

How to convert linq query to a list of string

my code is given below

C#
public static List<string> checksum(string cusip)
  {
      XDocument doc = XDocument.Load("SampleXML.xml");
      List<string> aacruedlist = doc.Descendants("PositionSummary")
                   .Where(item =>
                       {
                           string cus = (string)item.Element("Cusip");
                           return cus != null && cus == cusip;
                       })
                   .ToList().Descendants("AccruedInterest").ToList();//error
      return aacruedlist;
  }



Error i am getting

Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<System.Xml.Linq.XElement>' to 'System.Collections.Generic.List<string>'

please help

[edit]Code block fixed - OriginalGriff[/edit]
Posted
Updated 18-Jul-15 1:52am
v2

1 solution

The Descendents are XElement instances, not strings, so you need to Select the Value property in order to get strings:
C#
public static List<string> checksum(string cusip)
  {
      XDocument doc = XDocument.Load("SampleXML.xml");
      List<string> aacruedlist = doc.Descendants("PositionSummary")
                   .Where(item =>
                       {
                           string cus = (string)item.Element("Cusip");
                           return cus != null && cus == cusip;
                       })
                   .Descendants("AccruedInterest")
                   .Select(d => d.Value)
                   .ToList();
      return aacruedlist;
  }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900