-1

I have spent days (maybe weeks) trying to resolve this issue. I have two tables in which I am trying to organize my data into the following hierarchy. Here is an example of what this should like:

<ul><strong>PageTitle</strong>

<li>Category A</li>
<li>Category B</li>
<li>Category C</li>

I have my query running successfully in LinQPad, but I can't make it work in MVC. Here is the code I am using in MVC.

public IEnumerable<wl> List()
{
    IEnumerable<wl> wlTest;
    using (LibEntities _libEntity = new LibEntities())  {  
             wlTest = (from p in table1
                       join c in table2 on p.PK equals c.FK
                       group p by new { c.title } into g
                       select new wl
                        {
                          TitleName = g.Key.title,
                          Category = from p in g
                                     group p by g.cat_id into d
                                     select new wl{d.Key.cat_id}
                      }).ToList();

            return wlTest;

    }
}

Here is wl class info:

public class wl
{
    public string TitleName { get; set; }
    public string Category { get; set; }
}

I keep getting

Cannot initialize type 'LibraryProject.Model.wl' with a collection initializer because it does not implement 'System.Collections.IEnumerable'.

Okay, here's a very similar query in LinqPad.

from f_a_p in Find_article_progs
join f_a in Find_articles on f_a_p.Prog_num equals f_a.DbProg 
where f_a_p.Parent_id == 183
group f_a by new {f_a_p.Prog_title} into d
select new {
    d.Key.Prog_title, 
    Links = from f_a in d
            group f_a by new {f_a.DbTitle} into c
            select new {
                c.Key.DbTitle
            }
}

Can anyone help me with this?

ekad
  • 14,436
  • 26
  • 44
  • 46
DevHead19
  • 11
  • 3

2 Answers2

1

This is the problem:

select new wl{d.Key.cat_id}

It's not clear what you actually intended to do here (the meaningless type names really don't help) but you might want something like:

select new wl { CategoryId = d.Key.cat_id }

That's now an object initializer rather than a collection initializer.

It seems unlikely that you actually want new wl at all, mind you - given that you're already creating a wl. Likewise unless your Category property is actually a collection, you probably want to get a single entry from the group rather than all of a query's results.

If you could improve your naming and give more details about what you're trying to do (and your data hierarchy) we may be able to help you more.

(The query you've given won't with LINQPad either, by the way...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Okay, I cleaned up my code so hopefully things are clearer. Again I am using MVC. This code is slightly different than what I have in LinQPad but it is working as it should. The issue seems to be that I am dealing with a model in MVC. – DevHead19 Dec 30 '13 at 20:15
  • @user3147034: No, that's really *not* the problem. Your query is simply broken, in the way that I've said it is. Yes, a different query may well be working in LINQPad - but that's a different query. What do you expect `select new wl {d.Key.cat_id}` to do at all? – Jon Skeet Dec 30 '13 at 20:19
  • Yes, the new wl seems to be the problem. What alternatives do I have? If I don't use the "new wl" in LinQPad, things work splendidly, but in MVC the compiler complains "Cannot implicitly convert type "System.collections.Generic.IEnumerable to 'string'. – DevHead19 Dec 30 '13 at 20:27
  • @user3147034: Well we don't know what you're trying to do, so it's unclear how we can help you. *Why* were you using that in the first place? It would really help if you'd give us some sample input data and expected output data, because it's far too confusing at the moment. – Jon Skeet Dec 30 '13 at 20:29
  • I simply want to group my categories under a page title. The categories are in a different table than the page title. Since I can run the query in LinQPad I thought I could/should be able to run it in MVC. Anyways, thank for looking at my code. – DevHead19 Dec 30 '13 at 20:34
  • @user3147034: Well how about you show us the query that *does* work? Given that we've no idea what's in table1 or table2, it's really hard to follow your question. You should really look at rewriting the whole question - it's too unclear to answer for now. – Jon Skeet Dec 30 '13 at 20:36
  • Okay, I posted LinQPad query. I also removed new wl info but the compiler in MVC complains "Cannot convert type Generic.List to Generic.IEnumerable. Thank in advance. I apologize for any confusion - it's just that I have been at it for a long time. – DevHead19 Dec 30 '13 at 21:19
  • @user3147034: You *still* haven't told us what's in the tables, or given us sample input or expected output. The working LINQPad query only selects the title, not the category, so it doesn't show us what you're trying to do. I'm sorry, but at this point I give up - I can only ask for relevant information so many times. – Jon Skeet Dec 30 '13 at 21:22
0

in here don't type g.Key.title_name because g.Key is already your title name

TitleName = g.Key.title_name  // wrong
TitleName = g.Key // correct

And when selecting your categories I think you want to do something like this:

Categories = (from c in g 
               where c.TitleName == g.Key 
                 select c).ToList();

I don't know much about your entity, and your class names are killing me. So I can't give your more advice unless you provide me more information about your entities.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Okay, I cleaned up my code so hopefully things are clearer. Again I am using MVC. This code is slightly different than what I have in LinQPad but it is working as it should. The issue seems to be that I am dealing with a model in MVC. – DevHead19 Dec 30 '13 at 20:15
  • add your entity models which is relevant to your linq code.I can't help you unless i see the model – Selman Genç Dec 30 '13 at 20:17