Note that there are some explanatory texts on larger screens.

plurals
  1. POIQueryable returns null on invoking Count c#
    primarykey
    data
    text
    <p>I have a problem trying to get the count out of the following query:</p> <pre><code>var usersView = PopulateUsersView(); //usersView is an IQueryable object var foo = usersView.Where(fields =&gt; fields.ConferenceRole.ToLower().Contains("role")); </code></pre> <p>Where UsersView is a class which is populated from an EF entity called users (refer to the first line in the code above)</p> <p>This is the class definition for the UsersView class:</p> <pre><code>public class UsersView { public int UserId { get; set; } public string Title { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Street1 { get; set; } public string Street2 { get; set; } public string City { get; set; } public string PostCode { get; set; } public string CountryName { get; set; } public string WorkPlaceName { get; set; } public string Gender { get; set; } public string EMail { get; set; } public string Company { get; set; } public string RoleName { get; set; } public string ConferenceRole { get; set; } } </code></pre> <p>As I said trying to execute the line foo.Count() returns Null Exception and this might be because the ConferenceRole column allows Null in the database. </p> <p>Now what I can't understand is that when I invoke the same query directly on the ObjectQuery the Count of records (i.e. invoking foo2.Count()) is returned without any exceptions. </p> <pre><code>var foo2 = entities.users.Where(fields =&gt; fields.ConferenceRole.ToLower().Contains("role")); </code></pre> <p>Is it possible to the same query above but using the IQueryable usersView object instead?</p> <p>(It is crucial for me to use the usersView object rather than directly querying the entities.users entity)</p> <p>EDIT</p> <p>Below is the code from the PopulateUsersView method</p> <pre><code>private IQueryable&lt;UsersView&gt; PopulateUsersView() { using (EBCPRegEntities entities = new EBCPRegEntities()) { var users = entities.users.ToList(); List&lt;UsersView&gt; userViews = new List&lt;UsersView&gt;(); foreach (user u in users) { userViews.Add(new UsersView() { UserId = u.UserId, Title = u.Title, Name = u.Name, Surname = u.Surname, Street1 = u.Street1, Street2 = u.Street2, City = u.City, PostCode = u.Post_Code, CountryName = u.country.Name, WorkPlaceName = u.workplace.Name, Gender = u.Gender, EMail = u.E_Mail, Company = u.Company, RoleName = u.roles.FirstOrDefault().Name, ConferenceRole = u.ConferenceRole }); } return userViews.AsQueryable(); } } </code></pre> <p>Thanks</p> <p>UPDATE...</p> <p>Thanks guys I finally found a good answer to the difference between the IQueryable and the ObjectQuery objects. </p> <p>As a solution I am checking if the ConferenceRole is null and then checking with the contains method as many of you guys have said.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload