Sreedhar Danturthi
Sreedhar Danturthi

Reputation: 7571

problem with conversion of output from linq query to list<> in asp.net

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

Upvotes: 0

Views: 810

Answers (2)

fearofawhackplanet
fearofawhackplanet

Reputation: 53396

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.

Upvotes: 1

Blazes
Blazes

Reputation: 4779

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)

Upvotes: 2

Related Questions