Unable to set enum property during LINQ select

1.6k views Asked by At

I have the following code fragment

IQueryable<RssItem> rssItems = from prodotti prodotto in tuttiProdotti
                                               join marca in tutteMarche on prodotto.marca equals marca.id

                                               where prodotto.eliminato == 0
                                               orderby prodotto.id ascending
                                               select new GoogleShoppingRssItem
                                               {
                                                   Author = AUTHOR,
                                                   //Availability = prodotto.disponibilita > 0 ? AvailabilityType.instock : AvailabilityType.outofstock,
                                                   //Availability = AvailabilityType.instock,
                                                   Title = prodotto.titolo,
                                               };

When the first version of the availability line was uncommented I got ArgumentException, saying that value was not of type Edm.Int32. I thought it had something to do with prodotto.disponibilita property which is int? (but no null value is present on DB). The second version, not checking for product quantity, throws the same exception.

Only after commenting the second version of the availability setting the code finally worked and the expression got evaluated.

Enum is defined as follows:

  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [XmlType(Namespace="http://base.google.com/ns/1.0")]
    [XmlRoot("availability", Namespace="http://base.google.com/ns/1.0", IsNullable=false)]
    public enum AvailabilityType {

        /// <remarks/>
        [XmlEnum("in stock")]
        instock,

        /// <remarks/>
        [XmlEnum("available for order")]
        availablefororder,

        /// <remarks/>
        [XmlEnum("out of stock")]
        outofstock,

        /// <remarks/>
        preorder,
    }

Is there something I should know about setting an enum property in a LINQ select statement?

1

There are 1 answers

0
Ladislav Mrnka On BEST ANSWER

Is there something I should know about setting an enum property in a LINQ select statement?

Yes. EF4 doesn't support enums and you cannot use them in Linq-to-Entities query. It should work if you use them in Linq-to-Objects query so try this:

IQueryable<RssItem> rssItems = (from prodotto in tuttiProdotti
                                join marca in tutteMarche on prodotto.marca equals marca.id
                                where prodotto.eliminato == 0
                                orderby prodotto.id ascending
                                select prodotto).ToList() // Now you switched to L2O
                               .Select(x => new GoogleShoppingRssItem
                                               {
                                                   Author = AUTHOR,
                                                   Availability = x.disponibilita > 0 ? AvailabilityType.instock : AvailabilityType.outofstock,
                                                   Title = x.titolo,
                                               });