1

I tried searching on this, but can't seem to find anything that matches my LINQ query to use to help me figure this one out.

I'm getting a message in the debugger in the Results View->base object

+base {"Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context."} System.SystemException {System.NotSupportedException}

Here is my LINQ query (that returns a result fine in LINQPad...):

public IEnumerable<PendingItems> GetHazmatPendingShipping()
{
    var pending = context.HazmatInfoes
                         .Where(h => (h.ShippingFlag.Equals(false) && (h.ShippingType.Equals("Manual"))))
                         .GroupBy(x => new {x.ToBU, x.FromBU}, y => new {y})
                         .Select(p => p);
    return pending;
}

I know my return type is wrong. Will work on that after I figure out why this query fails to return a result.

My answer to this problem:

Since I had a key that was composite {string, string}, I had to create a class called PendingItems.

public IQueryable<PendingItems> GetHazmatPendingShipping()
    {
        IQueryable<PendingItems> pending = context.HazmatInfoes
            .Where(h => ((h.ShippingFlag.Value == false && h.ShippingType.Equals("Manual"))))
            .GroupBy(x => new {x.ToBU, x.FromBU}, y => y)
            .Select(p => new PendingItems {ToBu = p.Key.ToBU, FromBu = p.Key.FromBU, Items = p});
        return pending;
    }

The PendingItems class:

using System.Collections;
using System.Collections.Generic;

namespace Hazmat.Models
{
    public class PendingItems : IEnumerable
    {
        public string ToBu { get; set; }
        public string FromBu { get; set; }
        public IEnumerable<HazmatInfo> Items { get; set; }

        public IEnumerator GetEnumerator()
        {
            yield return this.Items;           
        }
    }
}

Thanks, Tim

P.S. This answer helped with this problem: https://stackoverflow.com/a/1775514/2733668

Community
  • 1
  • 1
CooperT
  • 63
  • 1
  • 11
  • looks like `LINQPad` sucks? – King King Oct 16 '13 at 19:29
  • 1
    Given that your return type is wrong the code shouldn't *compile*, so how are you getting a runtime error through the debugger? – Servy Oct 16 '13 at 19:31
  • I don't know why you said this works in `LINQPad` but your query has something wrong – King King Oct 16 '13 at 19:32
  • @McGarnagle isn't that we can practice `LINQ-to-SQL/EF` with `LINQPad`? – King King Oct 16 '13 at 19:33
  • Here is the exact LINQPad query that works for me: `HazmatInfo.Where(h => (h.ShippingFlag.Equals(false) && (h.ShippingType.Equals("Manual")))) .GroupBy(x => new {x.ToBU, x.FromBU}, y => new {y}) .Select(p => p)` – CooperT Oct 16 '13 at 19:38
  • @Servy, I have my return type in my code returning a new List so it compiles. I put a break there to look at my pending object to see why it was null. I know the return type here is wrong, but that's what I want to return ultimately. – CooperT Oct 16 '13 at 19:40
  • @McGarnagle, I am using EF with this project... I'm hoping this works with EF also. It would save a lot of headache/time. – CooperT Oct 16 '13 at 19:42
  • 3
    @CooperT `pending` is not an `IEnumerable` though, it's an `IEnumerable>`. – Servy Oct 16 '13 at 19:42
  • @Servy, it gives a result of `IEnumerable>`. But doesn't seem to like that format for the return type declaration. – CooperT Oct 16 '13 at 19:45
  • 2
    @CooperT your `.Select(p=>p)` is redundant, your query obviously returns `IEnumerable` – King King Oct 16 '13 at 19:46
  • @CooperT Yes, it's an entirely different type. Given that it, won't compile. Since it won't compile, discussing what it would do when run is rather pointless, since it *can't* run. – Servy Oct 16 '13 at 19:46
  • I doubt that the actual return type is `IQueryable`? – King King Oct 16 '13 at 19:48
  • Ok, then can you help me GET IT TO RUN? – CooperT Oct 16 '13 at 19:50
  • @CooperT oh, sorry - you're right. – McGarnagle Oct 16 '13 at 19:51
  • y => new {y} ? try y => y or just remove that – MRB Oct 16 '13 at 19:57
  • What are the data types of `ShippingFlag` and `ShippingType`? I think at least one of them does not match the type in the `Equals` call. – Gert Arnold Oct 16 '13 at 21:04

1 Answers1

2

This error occurs when there is condition regarding Nullable<> field, and comparison doesn't reflect that. Then primitive (false in your case) is converted to Nullable<> object, and exception is raised.

Probably ShippingFlag is of type Nullable<bool>, and assuming that you should rewrite your condition like this:

var pending = context.HazmatInfoes
    .Where(h => (!h.ShippingFlag.HasValue && h.ShippingFlag.Value.Equals(false)) && h.ShippingType.Equals("Manual"))
Krzysztof
  • 15,900
  • 2
  • 46
  • 76
  • ShippingFlag is a nullable type. I've queried only on `h.ShippingType.Equals("Manual")` and I've gotten valid results. So it does seem to be `ShippingFlag` that is causing the issue. I tried your addition to the query and it still fails and I get the same error of `Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.` The query returns valid results if I add `h.ShippingFlag.HasValue` but remove `h.ShippingFlag.Equals`. Thanks for the insight, at least we've now narrowed it down. – CooperT Oct 17 '13 at 12:35
  • Make sure you call `h.ShippingFlag.Value.Equals` not `h.ShippingFlag.Equals`. `HasValue` is only checking for NULL. – Krzysztof Oct 17 '13 at 12:45
  • 2
    Needed to change `h.ShippingFlag.Equals(false)` to `h.ShippingFlag.Value == false`. I should have known better as I've queried on that before that way! Thanks for the help! Now to figure how to declare the proper return type. – CooperT Oct 17 '13 at 12:47
  • Thanks for this answer. It saved me a lot of time. Btw: I got a nullable Guid and 'guid.Value.Equals' is still causing an error but 'guid.Value ==' is working. Maybe this helps others. – Bahamut Oct 21 '13 at 08:25