Help me filter my data!
October 12, 2009 6:59 PM
Subscribe
I'm trying to get more comfortable with Linq and I'm having some trouble. I have a list
myprojects that is initiated when a session is started (and discarded at the end). I have a
projects entity and repository and have a form setup to add
projects to
myprojects. This works. I have a
reports entity where there might be many reports to a single project. I'm trying to use myprojects to filter the reports and this does not work.
I'm using ASP.NET MVC, C#. Here are my domain model entities:
public class Report
{
public string Id { get; set; }
public string Description { get; set; }
}
----
public class myProjects
{
private List lines = new List();
IList Lines { get { return lines.AsReadOnly(); }
}
public class myProjectsLine
{
public Project Project { get; set;}
}
----
public class Project
{
public string Id { get; set; }
public string Description { get; set;}
}
I've been trying to filter in my controller. It might be preferable to filter in the domain, but I was just trying to get it to work, so please correct me if need be. Here's I have for my ReportController:
public class ReportsController : Controller
{
private IReportsRepository reportsRepository;
private IProjectsRepository projectsRepository;
public ReportsController(IProjectsRepository projectsRepository)
{
reportsRepository = new FakeyReportsRepository();
this.projectsRepository = projectsRepository;
}
public ViewResult List(Report reports)
{
var x = reportsRepository.Reports.ToList();
return View(x);
}
The List ViewResult works fine, it shows all the reports. If I create another ViewResult like this:
public ViewResult MyReport(myProjects myprojects, Report reports)
{
var a = myprojects.Lines.ToList();
var b = a.Count().ToString("d");
ViewData["my"] = b;
return View();
}
Hey it correctly shows the number of myprojects I have, so it must be seeing the data. I'm 95% sure that it has to do with myprojects being in the IList, yet trying to make it AsQueryable or turning reports ToList() won't get me anywhere when I'm trying to use Where and a Lambda expression like "var x = reports.Where(x=>x.Id == myprojects.Id)" I've been working on this for two days now and I think I might have worked myself into a corner and can't see how to do this. It seems like it should be so easy and I could write an SQL statement that does this very quickly, I'm assuming with all this Linq fanciness this should be doable.
posted by geoff. to computers & internet (4 comments total)
private List<> lines = new List
public IList<> Lines { get { return lines.AsReadOnly(); } }>
Without the spaces obviously.
posted by geoff. at 7:01 PM on October 12