4

I am trying to select the data I need into simple anonymous type to serialize the data for a Json request.

using (var dbContext = new DataContext())
{
    var vals = dbContext.Primaries.Select(p => new
    {
       Name = p.Name,
       Secondary = p.SecondaryId.HasValue ? new { Name = p.Secondary.Name } : null
    });
}

but when I call the enumerator on vals I get the following exception

Unable to create a null constant value of type 'Anonymous type'. Only entity types, enumeration types or primitive types are supported in this context.

I really actually do need Secondary to be null if the foreign key is null. How can I get an anonymous to be null straight from the select statement.

My idea solution is to be able to serialize the resulting data directly without having to process an intermediary data set.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • take a look here... the issue is with null http://stackoverflow.com/questions/682429/how-can-i-query-for-null-values-in-entity-framework – phil soady Jul 23 '13 at 01:58
  • looks like a dup to me – phil soady Jul 23 '13 at 02:00
  • This question has nothing to do with the other post you link to – Adrian Brand Jul 23 '13 at 02:42
  • so you arent having a null problem ? If you checked and are certain your issue is unrelated. fine. – phil soady Jul 23 '13 at 02:47
  • Can you select `p.SecondaryId.HasValue ? new { Name = p.Secondary.Name } : new { Name = (string)null }`? – Steven Wexler Jul 23 '13 at 03:26
  • That is the same as Secondary = new { Name = p.Secondary.Name } as even though the secondary table foriegn key is null the join still allows you to reference p.Secondary.Name and get null. I actually need Secondary to be null as I need the Json to be clean rather than polluting it with entries that don't exist. The nested properties go quite deep and I would rather just have null sent back in the Json rather than a load of useless scaffolding Json to represent a null value. – Adrian Brand Jul 23 '13 at 06:00
  • Did you ever get this solved? I'm running into the exact same issue. I'm left-outer-joining (Linq-to-Entities). I want to project the left-table into an anonymous entity. Because it's outer-joined, it may NULL. – harley.333 Nov 04 '13 at 17:06

2 Answers2

0

You can get around this by always returning the anonymous where the anonymous object may have null properties.

using (var dbContext = new DataContext())
{
    var vals = dbContext.Primaries.Select(p => new
    {
       Name = p.Name,
       Secondary = new { Name = p.SecondaryId.HasValue ? p.Secondary.Name : null }
    });
}

And if you really want to make Secondary null if p.SecondaryId is null you can add the following.

//ToList allows you to work with LINQ to Objects rather than Linq to Entities.  
//However, you'll generally want to call ToList() last for performance reasons.
var valsCleaned = vals.ToList()
                      .Select(v => { 
                                       if(v.Secondary.Name == null)
                                       {
                                           v.Secondary == null;
                                       }
                                       return v;
                                   });
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80
  • I don't want to enumerate to pocos first as then I would have to include all of the data in the secondary tables. The whole point of this is to only hit the database for what I need and return nice clean Json. – Adrian Brand Jul 23 '13 at 05:56
0

I don't have a "solution" per se. To workaround this issue, I simply projected the entire secondary entity. I'm not happy with this solution.

using (var dbContext = new DataContext())
{
    var vals = dbContext.Primaries.Select(p => new
    {
       Name = p.Name,
       Secondary = p.Secondary
    });
}

Obviously, projecting an entire entity is akin to "SELECT *" - which is a bad practice. And it may not work for you, depending on your actual query.

harley.333
  • 3,696
  • 2
  • 26
  • 31