0

I have a c# Asp.Net Web API server and I make post requests to get Json respone

I am working with Linq and have 2 tables:

First is a forumMessage which each message point to specific user with accountId:

public partial class forumMessage
{
    public int objectId { get; set; }        
    public string title { get; set; }
    public string body { get; set; }


    public int acountId { get; set; }//the user objectId
    public virtual user user { get; set; }
}

Second is the user:

public partial class user
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public user()
    {
        this.fora = new HashSet<forumMessage>();
    }

    public int objectId { get; set; }
    public string username { get; set; }
    public string password { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<forumMessage> fora { get; set; }//list of messages
}

so what I need is to response a json array like:

[{
"$id": "1",
"objectId": 2,
"title": "Message Title",
"body": "Message body",
"acountId": 1,
"user": {
    "$id": "2",
    "objectId": 1,
    "username": "Shay"
}}]

Notice that the respons is Without user password or the user list of messages Just some specific columns of user Thnx

INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
shay levi
  • 319
  • 3
  • 11

2 Answers2

1

Include returns everything, but you can use anonymous objects to return just a subset of data:

 from c in ctx.Customers
 join o in ctx.Orders on ..
 select new { c.ID, c.Name, o.OrderID, o.OrderDate };

Rough example, but the idea is the anonymous object defines the subset, and you can encode that object to JSON.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

You should convert your domain/business obejct (forumMessage) into DTO

Take a look at this

Community
  • 1
  • 1
Jarek Bielicki
  • 856
  • 6
  • 16