0

Hi I am trying to write a linq query in web api using etity framework. I have two entities

MainProj                            task_list
p_id                                t_id
p_name                              p_id
---------                           t_name
navigation properties            -------------------
task_list                         navigation properties 
                                  mainProj 

MainProj has 1 to many relation with task_list I am using following linq query

var list=( from z in MainProj 
select new pList
{
       proj_name=z.p_name
       proj_id=z.p_id
       newtasklist = new List<newtasklist>
         {
             new newtasklist {
             task_list=z.task_list.(** cant access the properties from task_list here)
    }}
  });

classes are

    public class pList
   {
       public int p_id { get; set; }
       public string p_name { get; set; }
       public IEnumerable<newtasklist> newtasklist { get; set; }

  }

   public class newtasklist
  {
       public int t_id { get; set; }
       public string t_name { get; set; }
        public int p_id { get; set; }
       public pList pList {get; set;}
   }

Please let me know how i can access task_list properties at this line in linq query

 task_list=c.task_list.(** cant access the properties from task_list here)
5
  • What do you want newtasklist to contain? Commented Mar 21, 2014 at 7:10
  • Where is that c.task_list reference coming from? There is no reference for c Commented Mar 21, 2014 at 7:14
  • Basically the linq query should return for each p_id and p_name all the tasks(newtasklist) containing t_id ad t_name. Commented Mar 21, 2014 at 7:20
  • Sorry that was suppose to be z.task_list it was a typo. Thanks Commented Mar 21, 2014 at 7:20
  • Not sure why are you assigning a task_list in the newtasklist = new List<newtasklist> { new newtasklist { task_list=z.task_list ?? task_list is not a member of the newtasklist class Commented Mar 21, 2014 at 7:49

1 Answer 1

2

As you said there is one to many relation in Mainproj and task_list you can do something like following.

Here is a list of Mainproj

List<MainProj> mProjects = new List<MainProj>
{
    new MainProj
    {
        p_id = 1, taskList =new List<task_list> {
                    new task_list{ p_id=1, t_id = 1, t_name="Dev"},
                    new task_list{ p_id=1, t_id = 2, t_name="QA"},
                }, name="Proj1"
    },
    new MainProj
    {
        p_id = 2, taskList =new List<task_list> {
                    new task_list{ p_id=2, t_id = 1, t_name="RA"},
                    new task_list{ p_id=2, t_id = 2, t_name="DEV"},
                },name="Proj2"
     }
};

And following is the LINQ

  var lst = (from p in mProjects
             select new pList
             {
                 p_id = p.p_id,
                 p_name = p.name,
                 newtasklist = p.taskList.Select(x => new newtasklist { p_id = x.p_id, t_id = x.t_id, t_name = x.t_name })
             });

here is the Quick watch result

Quick watch

Sign up to request clarification or add additional context in comments.

Comments

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.