views:

79

answers:

2

I have two Repeater Control

var list = from i in DA.obm_view_studentLists where i.FranchiseID == FranchiseID && i.ExamDate == DateTime.Parse(this.pallavi.DropDownSelectedValue) select i;

I want to get result in 1 go from database.

this._approvedStudentList.DataSource = list.Select(e => e.ApprovedByOBM == true);

this._pendingStudentList.DataSource = list.Select(e => e.ApprovedByOBM == false);

I have a Label Field (UnPaidTotal) in pendingStudentList Repeater where I want to display Total Fee Pending

I tried but fail

UnPaidTotal = string.Format("{0:c}", list.Select(e => e.ApprovedByOBM == true).Sum(j => j.CourseFee));
+2  A: 

Evaluate list to get all results in one shot -- just wrap from i in… select i in parentheses and add .ToList() at the end.

When setting the DataSource, replace list.Select(… with list.Where(…

Same thing when getting the unpaid total -- use Where instead of Select.


You don't need to return two collections from the database, because you're dividing the results based on a boolean value. You want all of the query results, because no matter what each result will belong to one collection or the other, so you can split them in-memory.

Select is used to perform a transformation on a collection, whereas Where is a filter. An example of select would be manufacturerList = carCollection.Select(car => car.Manufacturer);. This takes a collection of cars, and transforms it into an enumerable of Manufacturer, by selecting out just that property from each member of the collection.

Jay
+1  A: 

The key here is that after your first line of code where you create the query for the list variable you haven't gone out to the database yet. All you did is create an IEnumerable/IQueryable that knows how to go out to the database and get your data. Nothing happens until you actually bind it to a control or otherwise try to iterate over the results.

Your problem is that when you do finally bind to the first control the state of that IEnumerable changes such that it now points to the end of the result set rather than waiting to open the database at the beginning. When you try to bind the second repeater there are no records there.

The easiest way to remedy this is to simply put the original query results in a list, though you should be aware that this will cause your web server to load the entire result set into RAM. Another option is to use one repeater, order the query appropriately and use code to output the desired html depending on what kind of row you have and the extra html between sections. This will perform better but crosses lines between separate tiers.

Joel Coehoorn