Loop to LINQ Conversion -

Posted by Pino on Stack Overflow See other posts from Stack Overflow or by Pino
Published on 2010-05-25T15:02:34Z Indexed on 2010/05/25 15:11 UTC
Read the original article Hit count: 302

Filed under:
|
|
|

Ok I have the following, set-up and working great. These lines of code should do a conversion from DAL Entity (Subsonic) to a ViewModel.

    IList<ProductOptionModel> OptionsRetData = new List<ProductOptionModel>();

    foreach (var CurProductOption in this.ProductOptions)
    {
        OptionsRetData.Add(CurProductOption.ToDataModel());
    }

    returnData.Options = OptionsRetData.AsEnumerable();

I'd like to convert this to a LINQ single line statment and came up with the following.

returnData.Options = this.ProductOptions.Select(o => o.ToDataModel());

and am recieving the following error.

Server Error in '/' Application.
Sequence contains no matching element 

So why does the first statment work but not the LINQ and, what steps can I take to resolve it.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET