C Sharper
C Sharper

Reputation: 8646

Serialize Object Linq to SQL

I have following table structure in LINQ:

enter image description here

objective_id is foreign key in Resource table which is PK in Objective table.

I wanted to have output like this:

{
    ID: '123',
    user_id: '1',
    skill_id:'1',
    name:'abcs'
    ...
    objective_resource: 
    {
        ID: '123',
        ...
    }
}

For this i tried:

 using (DataTableClassesDataContext context = new DataTableClassesDataContext())
            {
                DataLoadOptions opts = new DataLoadOptions();
                opts.LoadWith<objective_resource>(u => u.objective_id);
                context.LoadOptions = opts;

                return new JavaScriptSerializer().Serialize(context.objective_resources
                    .Where(u => u.id == 1)
                    );
            }

But It given output:

        {
            ID: '123',
            ...

objective:
  {

        ID: '123',
        user_id: '1',
        skill_id:'1',
        name:'abcs'
        ...

    }
 }

I wanted to have:

{
        ID: '123',
        user_id: '1',
        skill_id:'1',
        name:'abcs'
        ...
        objective_resource: 
        {
            ID: '123',
            ...
        }
    }

Please help me.

How can i get the output???

Upvotes: 2

Views: 140

Answers (1)

Shital Kadam
Shital Kadam

Reputation: 238

make the change in association property in dbml file as

enter image description here

and write the code as

var data= dataContext.objectives;
return new JavaScriptSerializer().Serialize(data);

Upvotes: 1

Related Questions