0

I have a Software titles class which is defined as follows:

public class SoftwareTitles
{

string softwareTitle;
string invoiceNumber;

public SoftwareTitles(string softwareTitle, string invoiceNumber)
{
    this.softwareTitle = softwareTitle;
    this.invoiceNumber = invoiceNumber;
}

public string InvoiceNumber
{
    get
    {
        return this.invoiceNumber;
    }
}

public string SoftwareTitle
{
    get
    {
        return this.softwareTitle;
    }
}

}

and i'm getting software titles and invoice numbers from my linq query which i want to store in a list using the following code:

   List<SoftwareTitles> softwareTitlesList = new List<SoftwareTitles>();
var result = (from CustomersRecord custRecords in custRecordContainer select new { InvoiceNumber = custRecords.InvoiceNumber, SoftwareTitle = custRecords.InvoiceNumber }).ToList();
        softwareTitlesList = result;

But it is freaking out giving me this error:

Error   1   Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<SoftwareTitles>'  

Can any one help me ?

Thanks in anticipation

1
  • This has nothing to do with the answer, but notice that you are creating your objects using 2 custRecords.InvoiceNumber. It looks like you intended: {custRecords.SoftwareTitle, custRecords.InvoiceNumber}
    – Blazes
    Apr 20, 2011 at 11:53

2 Answers 2

2

I think the problem is that you are creating an anonymous type:

select new { InvoiceNumber = custRecords.InvoiceNumber, SoftwareTitle = custRecords.InvoiceNumber }

and you are trying to build a list of SoftwareTitles. I am not 100% on the syntax, but try using:

select new SoftwareTitle( custRecords.SoftwareTitle, custRecords.InvoiceNumber)
1

Your select code

select new { 
       InvoiceNumber = custRecords.InvoiceNumber, 
       SoftwareTitle = custRecords.InvoiceNumber 
}

is returning an annonymous type. You can't put your annonymous type into a List<SoftwareTitles>.

Two solutions:

1) You can select an annonymous type if you let the compiler determine the type of your list using the var keyword

var myList = from CustomersRecord custRecords 
              in custRecordContainer 
               select new { 
                   InvoiceNumber = custRecords.InvoiceNumber, 
                   SoftwareTitle = custRecords.InvoiceNumber 
             }).ToList();

2) Map to a SoftwareTitle object in your Select

List<SoftwareTitle> myList = from CustomersRecord custRecords 
                              in custRecordContainer 
                               select new SoftwareTitle { 
                                  InvoiceNumber = custRecords.InvoiceNumber, 
                                  SoftwareTitle = custRecords.InvoiceNumber 
                               }).ToList();

I would guess you probably want to do it the 2nd way. Using a list of annonymous type is only really useful as an intermediate step in a function, as you generally can't pass it as a function paramater somewhere else.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.