1

Hi I have following two model classes

public class c1
{
    public int id { get; set; }
    public int ptId { get; set; }
    public int bId { get; set; }
    public int rId { get; set; }
    public IEnumerable<styles> newStruct { get; set; }
}

public class styles
{
    public int id { get; set; }
    public int bId { get; set; }
    public string desc { get; set; }
}

I am trying to write a linq query

var records = (from y in db.main
               join c in db.secondary on y.bId equals c.bId
               where c.id == id
               select new c1
               {
                   pId= c.pId,
                   id = c.id,
                   newStruct = new List<styles>
                   {
                       new styles
                       {
                           id=y.room_id,
                           desc=y.desc,
                       }
                   }
               });

return records.ToList();

Problem I am having is that in newStruct is suppose to be List of all the styles but it just returns one style at a time rather than one list. Please let me know how can it return record where inside it contains list of styles Thanks

2 Answers 2

3

If you want to get a sublist by main list, you should use group by,

You can try this, but I'm not sure it worked. Becasue I couldn't compile it.

var records = (from y in db.main
                           join c in db.secondary on y.bId equals c.bId
                           where c.id == id
                           group c by new
                           {
                               c.pId,
                               c.id
                           } into gcs
                           select new c1
                           {
                               pId = c.Key.pId,
                               id = c.Key.id,
                               newStruct = from g in gcs select new styles { id=g.room_id, desc=g.desc}
                           });
Sign up to request clarification or add additional context in comments.

Comments

0

Is this LINQ to Entities? If so, and the mappings are correct in the edmx, you can try:

var records = 
    from c in db.secondary
    where c.id == id
    select new c1
    {
        pId = c.pId,
        id = c.id,
        newStruct = c.main.Select(m => new styles { id = m.room_id, desc = m.desc })
    };

return records.ToList();

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.