1

I am new to the Entity Framework. I am attempting to get a count from a LINQ query that should return 1 result. The count is always 0 though. I know that the data is there and the relationship is sound in the edmx.

Here is the code:

LineItem li = order.LineItems.Where(i => i.ID == lineItemId).FirstOrDefault();
if (li != null)
{
    if (li.Notes.Count > 0)
    {
        // Get note data
    }
}

Now ListItem is not null so I am able to get to the count call. Again I have verified that I have my data as my debugging nets the .Where match as "52635 == 52635"

Is there something I am missing here in order to get my count? Or is there any suggestion to how I can debug this problem further?

I am using Entity Framework 4 +

Thanks!

4
  • Have you stepped into a debugger and examined li.Notes at the point where you're calling Count on it? I suspect the issue is there, and not with the count property. Commented Mar 13, 2012 at 13:05
  • Show the code that retrieves order, the problem is probably there (as alluded to by @DanielAWhite's answer). Commented Mar 13, 2012 at 13:08
  • Do I need a .Include if I am able to get to my Notes through the Navigation Property? The relationship is there and I get an error when I try to use the .Include. Commented Mar 13, 2012 at 13:20
  • Notes are not loaded unless u specify it. be sure about the entity name: is it 'Notes' or 'LineItemNotes' Commented Mar 13, 2012 at 13:24

3 Answers 3

2

Make sure you Include notes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. When I try that: LineItem li = order.LineItems.Include("LineItemNotes").Where(i => i.ID == lineItemId).FirstOrDefault(); I get: CS0122: 'System.Data.Objects.DataClasses.RelatedEnd.Include(bool, bool)' is inaccessible due to its protection level
1

+1 on Include notes. When you've done that you should use the more efficient if(li.Notes.Any()) in place of if(li.Notes.Count() > 0).

Also, you may find that using li.Notes.Count which is a property on the collection type used by Notes won't work in the same way as li.Notes.Count() which is a Linq extension method. I'm not convinced without checking that you need Include.

Comments

0

I found that this is an issue with the record in the DB being deleted. Sorry guys! Thanks for the help though!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.