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 answers total) 1 user marked this as a favorite
 
Response by poster: Oops, quick correction under the myprojects entity it should read like this:

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, 2009


Response by poster: Dammit, between the carets is supposed to be myprojectsline.
posted by geoff. at 7:02 PM on October 12, 2009


Response by poster: :(
posted by geoff. at 1:53 PM on October 16, 2009


Best answer: For many-to-many you have to use EntityRef which creates a one-to-many relationship. This works but EntityRet does not get translated to SQL so if I do something like projectsrepository.Projects where c.MyEntityRef.Count() > 0 it won't run, but it will show if you do projectsrepository.Projects.ToList(), so the idea is to filter on the entityref but I haven't figured out how to do that.
posted by geoff. at 10:36 AM on October 20, 2009


« Older What to do in Ithaca   |   Thanksgiving in a log cabin on a lake - but where? Newer »
This thread is closed to new comments.