Usando una expresión Lambda en una Vista en MVC3

The situation that I am trying to overcome is, seemingly, simple but I am not getting the results I am looking for. I have an EDMX file serving as the Models for my particular application and am using the Razor engine.

In the EDMX file I have a view which is a roll up of a data from several tables that looks like this:

1 1 1

1 2 2

1 3 3

2 1 1

2 2 2

3 1 1

So the first three records belong to detail 1, the next two to detail 2, and the final record belongs to detail 3. However I want to 'turn' this data so that it displays on the web like this:


--- 1

--- 2

--- 3


--- 1

--- 2


--- 1

In my view I am doing some very basic stuff:

     foreach(var item in Model){
         var items = Model.Where(i => i.ID == item.ID);
         foreach(var itm in items){
           //construct UI loop

The issue that I am having is that while the Lambda expression is returning the correct number of rows 'items'always contains the value of 'item' repeated n number of times where n is the number of rows associated with the given ID.

Can someone explain what I am doing wrong and why this doesn't work as I think it should?


preguntado el 02 de mayo de 12 a las 19:05

I couldn't provide concrete code without seeing what your model looks like, but I think what you're looking for is the GroupBy Linq query. -

1 Respuestas

The code provided is correct. The problem here is that if records have the same EntityKey in the EDMX file then they are considered the same record and are displayed as such. This was the problem I had. I added a GUID column to the view and removed the EntityKeys from the other columns in the view and the above code worked fine.

contestado el 02 de mayo de 12 a las 20:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.