Jim Blum
Jim Blum

Reputation: 2646

Use index as an extra id in linq query

Let's suppose that I execute the following query in C#

    var query = from b in db.SchoolTestsPerModulePerStudent
                where b.Student.Equals(2)
                select b;

Ok, I get as results all the records of my model SchoolTestsPerModulePerStudent. However, Is it possible, to add a new Id when executing the query?

That means that if my model has 4 fields, StudentID, ModuleID, DateTest and GradeTest, to get the following

1, 2, 3, 1 January 2013, 76

2, 2, 3, 5 January 2013, 79

3, 2, 4, 1 January 2013, 73

4, 2, 4, 7 January 2013, 71

How I can do that?

Upvotes: 2

Views: 332

Answers (4)

Dave Zych
Dave Zych

Reputation: 21887

There is an overload of Select that gives you the index:

var resultsWithId = query.Select((q, i) => new { Id = i, Student = q });

This will put the results in an anonymous object.

EDIT: As Nico mentions below, this might be hard to use as a ViewModel. If you want to use it as that, create a new defined class that can hold the info:

public class MyClass
{
    int Id;
    int StudentId;
    //etc
}

var resultsWithId = query.Select((q, i) => new MyClass { 
                                                 Id = i + 1, //one based index 
                                                 StudentId = q.StudentId 
                                                 //etc
                                               });

Upvotes: 5

chenZ
chenZ

Reputation: 930

i think you can use Select().

source.Select(x=>new Model{newid=something,p1=x.p1,p2=x.p2....})

or

source.Select(x=>new{newid=something,x.p1,x.p2.....})

or if you want newid is index,you can use

source.Select((x,index)=>new{newid=index,x.p1,x.p2.....})

you can do more linq after select function

Upvotes: 1

Nico
Nico

Reputation: 12683

Are you trying to just add an extra property to your class to be returned to your model? If you need a concrete class structure for your View you will need to create a new model with this new property as anonymous types wont be useful in a View Model.

public class MyNewModel
{
    public int Id { get; set; }
    public long StudentID { get; set; }
    public long ModuleID { get; set; }
    public DateTime DateTest { get; set; }
    public int GradeTest { get; set; }
}

Then you will need to convert your orignal query to a List() and select your new model type setting the value.

var query = (from b in db.SchoolTestsPerModulePerStudent
            where b.StudentID.Equals(2)
            select b).ToList();

var newResults = query.Select(x => new MyNewModel
{
    DateTest = x.TestDate,
    StudentID = x.StudentID,
    GradeTest = x.TestResult,
    ModuleID = x.ModuleID,
    Id = query.IndexOf(x)
});

It also may be quicker to just do this in your view by passing the query as a List to your view and using the IndexOf() method while enumerating the set.

Upvotes: 1

Claies
Claies

Reputation: 22323

if you convert your query to a list using .ToList(), you can use query.IndexOf() to get the 0 based index of the item in the list.

Upvotes: 1

Related Questions