0

I don't know how I can accomplish this task I have tried numerous ways and all come up with one error or another. the few ways I have tried that did not error out on my instead just did not provide me with the results I was looking for.

Example of what I am looking for in end result

{
    'type': 'NamedAA',
    'id': '63c0f27a-716e-804c-6873-cd99b945b63f',
    'x': 80,
    'y': 59,
    'width': 99,
    'height': 107,
    'userData': {

    },
    'cssClass': 'DBTable',
    'bgColor': '#DBDDDE',
    'color': '#D7D7D7',
    'stroke': 1,
    'alpha': 1,
    'radius': 3,
    'name': 'DuringHoursAutoAttendant',
    'entities': [
        {
            'text': 'id',
            'id': '49be7d78-4dcf-38ab-3733-b4108701f1'
        },
        {
            'text': 'employee_fk',
            'id': '49be7d78-4dcf-38ab-3733-b4108701fce4'
        }
    ]
}

The code that is giving me error

  var aahope=new JObject(
    new JProperty("type", "NamedAA"),
    new JProperty("id",aaid),
    new JProperty("x",80),
    new JProperty("y",59),
    new JProperty("width",99),
    new JProperty("height",107),
    new JProperty("userData",new JObject()),
    new JProperty("cssClass", "DBTable"),
    new JProperty("bgColor", "#DBDDDE"),
    new JProperty("color", "#D7D7D7"),
    new JProperty("stroke",1),
    new JProperty("alpha",1),
    new JProperty("radius",3),
    new JProperty("name",""),
    new JProperty("entities", new JArray(
        (from e in db.HostedVoiceAAKeys
        where e.HostedVoiceAAID == aaid.HostedVoiceAAID
        select new JObject(
                    new JProperty("id",e.OptionKey),
                    new JProperty("text",e.OptionGuid))).ToArray()
        ))
    );

Error Message:

Only parameterless constructors and initializers are supported in LINQ to Entities.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Only parameterless constructors and initializers are supported in LINQ to Entities.

Source Error: 

Line 2763:            var aaresults = "";
Line 2764:            var aahope=new JObject(
Line 2765:                new JProperty("type", "NamedAA"),
Line 2766:                new JProperty("id",aaid),

2 Answers 2

3

No need to to deal with JObject/JProperty classes. Just create an object from these classes

public class Entity
{
    public string text { get; set; }
    public string id { get; set; }
}

public class RootObject
{
    public string type { get; set; }
    public string id { get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public UserData userData { get; set; }
    public string cssClass { get; set; }
    public string bgColor { get; set; }
    public string color { get; set; }
    public int stroke { get; set; }
    public int alpha { get; set; }
    public int radius { get; set; }
    public string name { get; set; }
    public List<Entity> entities { get; set; }
}

then serialize your object

 string json = JsonConvert.SerializeObject(rootObj);
Sign up to request clarification or add additional context in comments.

3 Comments

Constract an object ... that sounds kinky. ;)
So I actually do have classes already made but my trouble with them is getting the entities included with the RootObject. This is all querying a database with one RootObject to many Entities. your suggestion here although sounds great it is missing the actually pulling in the data
So after finally fixing another issue that stopped me from testing this out correctly I found this actually was the best answer and works beautifully.
0

LINQ to Entities does not allow you to instantiate an object inside of Select() using a constructor with parameters.

You can change your LINQ statement to execute the query using ToList(), then project it into JObjects using Select()...

new JArray(
        (from e in db.HostedVoiceAAKeys
        where e.HostedVoiceAAID == aaid.HostedVoiceAAID)
            .ToList()
            .Select(h => new JObject(
                    new JProperty("id",e.OptionKey),
                    new JProperty("text",e.OptionGuid)))
            .ToArray()
     )

Although as the L.B.'s answer suggests, creating custom classes would be the preferred approach here.

3 Comments

This solution actually error on me Non-static method requires a target. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Reflection.TargetException: Non-static method requires a target.
So after research on that error message with EF it appears it is always related to a null value, I will be double checking my results and let you know the outcome
I did not test specifically this solution out as L.B's seems to work beautifully now that I fixed the error I was getting which was unrelated to the code either of you suggested. Now maybe I can move forward and get this project going. BTW that error message I was getting was due to a null value in the where clause. FACEPALM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.