0

Hi I am writing linq query to get data from multiple tables. I am trying to get array of objects as below.

   processobject retObj = new processobject();
 retObj =(from c in entityObject.NCT_Process
    join user in entityObject.NCT_UserRegistration on c.createdUserId equals user.User_Id
     join file in entityObject.NCT_FileUpload on c.iconfileId equals file.upld_ID 
    join templObj in entityObject.NCT_Templates on c.ID equals templObj.processId
   where c.processid == "10"
   select new processobject
    {
    id = c.ID,
    code = c.code,
    flochartContent = c.flowchartContent,
    //arrayofTemplates array i want to return
    }
    );
    public class processobject
    {
    public templatesObject[] arrayofTemplates { get; set; }
    public int id { get; set; }
    public string flochartContent { get; set; }
    public string code { get; set; }
    }

In the above query i have written condition where c.processid == "10". so there are multiple records exists in NCT_Templates with processid 10. I want to return array with all those records. I have created array as below in my custom class. I have this query

 processobject retObj = new processobject();
retObj = (from c in entityObject.NCT_Process
                                  join user in entityObject.NCT_UserRegistration on c.createdUserId equals user.User_Id
                                  join file in entityObject.NCT_FileUpload on c.iconfileId equals file.upld_ID
                                  into filesObjFirst
                                  from wt1 in filesObjFirst.DefaultIfEmpty()
                                  join templObj in entityObject.NCT_Templates on c.ID equals templObj.processId
                                  into filesObj
                                  from wt in filesObj.DefaultIfEmpty()
                                  where c.ID == dbObject.ID
                                  select new processobject
                                  {

                                      id = c.ID,
                                      code = c.code,
                                      flochartContent = c.flowchartContent,
                                      name = c.name,
                                      parentId = c.parentId,
                                      projectId = c.projectId,
                                      objectives = c.objectives,
                                      displayOrder = c.dispalyOrder,
                                      iconFileId = c.iconfileId,
                                      level = c.level,
                                      iconFileobj = new iconFile
                                      {
                                          id = wt1.upld_ID,
                                          name = wt1.fileName,
                                          url = wt1.filePath
                                      },
                                      description = c.description,
                                      startCriteria = c.startCriteria,
                                      endCriteria = c.endCriteria,
                                      reporting = c.reporting,
                                      output = c.output,
                                      kpi = c.kpi,
                                      procedureHistory = c.procedureHistory,
                                      role = c.role,
                                      duration = c.duration,
                                      owner = c.owner,
                                      visibility = true,
                                      createdUserId = c.createdUserId,
                                  }).FirstOrDefault();

I have this one more query

 templatesObject[] templatesobject = (from c in entityObject.NCT_Templates
                                                             where c.processId == dbObject.ID
                                                             join file in entityObject.NCT_FileUpload on c.templateFileId equals file.upld_ID
                                                             into filesObjFirst
                                                             from wt1 in filesObjFirst.DefaultIfEmpty()
                                                             select new templatesObject
                                                             {
                                                                 id = c.id,
                                                                 title = c.title,
                                                                 version = c.version,
                                                                 visible = c.visibility,
                                                                 filesObj = new iconFileTemplate()
                                                                 {
                                                                     id = wt1.upld_ID,
                                                                     url = wt1.filePath,
                                                                     name = wt1.fileName
                                                                 }
                                                             }).ToArray();

Inside arrayofTemplates I want to store records of NCT_Templates. Any help would be appreciated. Thank you.

1 Answer 1

2

A .ToArray() will do the job after your query.

   processobject retObj = new processobject();
 retObj =(from c in entityObject.NCT_Process
    join user in entityObject.NCT_UserRegistration on c.createdUserId equals user.User_Id
     join file in entityObject.NCT_FileUpload on c.iconfileId equals file.upld_ID 
    join templObj in entityObject.NCT_Templates on c.ID equals templObj.processId
   where c.processid == "10"
   select new processobject
    {
    id = c.ID,
    code = c.code,
    flochartContent = c.flowchartContent,
    //arrayofTemplates array i want to return
    }
    );

var arr = retObj.ToArray();

Update

I Recommend you use ICollection instead of an Array. since you wanted the array the solution is explained above, but its more understandable if you use ICollection:

public class processobject
{
public ICollection<processobject> processobject{ get; set; }
public int id { get; set; }
public string flochartContent { get; set; }
public string code { get; set; }
}

then create a collection and pass it to the property like:

var list = retObj.ToList();

Update

Since the Array in the json object returns properties, i recommend you create another model. and pass the value directly to an object of this class. like below:

public class processobject
{
public templateFileId templateFileId{ get; set; }
public int id { get; set; }
public string flochartContent { get; set; }
public string code { get; set; }
}

public class templateFileId
{
  public int id {get; set;}
  public string url {get; set;}
  public string name {get; set;}
  public int created {get; set;}
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. I edited my question. I do not know how can i assign values to the properties. I have posted sample object above.
@NiranjanGodbole updated :), remember if the array returns more than one set of data u need to use ICollection as above
Thanks for your time. templateFileId will be having more than one rows in my case. How can i assign values to properties of templateFileId. For example i have written id = c.id, title = c.title,version = c.version so like this how can i assign values to url,name etc
@NiranjanGodbole In your query. you can do a select based on TemplatefileId, assign the data to new class like : new templateFileId { id = c.Id, url = c.Url, name = c.Name, created = c.Created }
already i have select new processobject how can i add new templateFileId
|

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.