Jared
Jared

Reputation: 2483

C# LINQ to Entity .Count

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!

Upvotes: 1

Views: 908

Answers (3)

Jared
Jared

Reputation: 2483

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

Upvotes: 0

Phil
Phil

Reputation: 43021

+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.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191058

Make sure you Include notes.

Upvotes: 2

Related Questions