[RESOLVED]Eager Load – Determine if ICollection Empty

I have a Person table and each person can have many or no Papers.

So there is a one to zero or many relationship between Person and Papers

How can I determine if a person has at least one paper in the where clause below of my controller?

public ActionResult PaperImages(string searchTerm = null, int page = 1)
        {
            if (page < 1)
            {
                return HttpNotFound();
            }

            var people= db.Person
                .Include(p => p.Papers)
                .Where(p => p.Department_id == 496

                    && p.Papers.Select(p => p.File_Name.Contains("."))

                    && (searchTerm == null || p.Last_Name.Contains(searchTerm)))
                .Select(a => new PersonListViewModel
                {
                    Person_ID = p.Person_ID,
                    Last_Name= p.Last_Name,
                    Papers= p.Papers
                })
                .ToPagedList(page, 10);

.
.
.
.
.
            return View(people);

 

I am having difficulty with the following line of code:

                    && p.Papers.Select(p => p.File_Name.Contains("."))

I want to determine if the Person has any Papers in the where clause – if not then do not show that person on this list.

I just ended up using

&& p.Papers.Count >= 1

Leave a Reply