score:74
you can't compare a related table to null
directly. instead, compare against your foreign key member (assuming that primarytable
reference relatedtable
using a member called relatedtableid
.
sites.where(s => s.relatedtableid != null && s.relatedtable.any(
p => p.name == param1.tolower() && p.policytype == "primary"));
you may even be able to get away with removing the null check completely. since this query is run against the database, you won't get a nullreferenceexception
and it may work. you'll have to double check on that though.
score:-1
it work me , i just remove the null check;
correct: result=
db.emptable.firstordefault().projectsassign.name,
incorrect : result=
db.emptable!=null && db.emptable.firstordefault().projectsassign!=null ?
db.emptable.firstordefault().projectsassign.name : null,
score:0
i don't have a foreign key field configured because the relationship between maintable
and relatedtable
in my case is 1 to 1. however for a 1 to many relationship if you don't have foreign key but you have a navigation property to the maintable
model in the relatedmodel
model the following solution also works.
1 to 1
var result = from s in context.sites
join r in context.relatedtable on s.id equals r.id
select s;
return result;
1 to many
var result = from s in context.sites
join r in context.relatedtable on s.id equals r.site.id
into rs
where rs.relatedtable.any(p => p.name == param1.tolower() && p.policytype == "primary")
select s
score:5
collection field can be null in this case you get exception nullreferenceexception
when use relatedtables.any()
if you add relatedtables != null
as in a question then you can get
cannot compare elements of type 'system.collections.generic.icollection`1[[project1, version=1.0.0.0, culture=neutral, publickeytoken=null]]'. only primitive types, enumeration types and entity types are supported.
if you get the nullreferenceexception
exception, lazy loading is not turned off and you are good with lazy loading for the field then to prevent exception mark field with virtual
keyword to allow lazy loading for the field
virtual icollection<table> relatedtables{ get; set; }
score:11
the error can occur if navigation collection compared with null. it should be checked if any record exist. in the particular example any is used anyway, so check collection to null is redundant
incorrect
dbcontext.maintable.where(c => c.relatedtable==null )
correct
dbcontext.maintable.where(c => !c.relatedtable.any() )
score:14
it is because that you have a null check in the where clause.
Source: stackoverflow.com
Related Query
- Cannot compare elements of type 'System.Collections.Generic.ICollection`1 Only primitive types, enumeration types and entity types are supported
- Cannot implicitly convert type system linq IQueryable to system collections generic List
- c# asp.net: Cannot compare elements of type 'System.Int32[]'. Only primitive types, enumeration types and entity types are supported.'
- Cannot compare elements of type 'System.Linq.IQueryable`1'. Only primitive types, enumeration types and entity types are supported
- System.NotSupportedException - Cannot compare elements of type 'System.Linq.IQueryable
- Calling generic method with a type argument known only at execution time
- I need to compare two very large collections with potentially missing elements
- Cannot convert source type to target type List<KeyValuePair> Linq
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Cannot convert source type system.nullable to target type int
- Type cannot be inferred within a generic delegate
- Cannot implicitly convert type System.Linq.IQueryable to System Collections.Generic.IEnumerable
- Implementing IComparable<T> Interface for a generic class to compare type T
- The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to ''
- Generic lists: The type arguments for method cannot be inferred from the usage in lambdas
- Use LINQ to select elements within a generic list and casting to their specific type
- How can I compare two generic collections
- EF code first cannot convert type to int
- How to remove error “Cannot compare elements of type 'System.Linq.IGrouping`2”
- how to compare values between two collections and only render distinct values within a listbox using asp.net
- System.Linq.Dynamic.Core cannot compare 'object' type
- Compare two generic type in linq to sql
- Cannot implicitly convert type IQueryable to ObjectQuery with nested collections
- Cannot convert source type 'System Linq IQueryable<decimal>' to target type decimal
- MVC many to many get entities with of certain type only in code first
- Need to compare 2 generic list of same type based on dynamic columns/fields that will be know at run time
- Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query
- How to use linq to compare only the same day, but in my tale filed only have time type field
- Quickest way to compare two generic lists for differences
- Unable to create a constant value of type Only primitive types or enumeration types are supported in this context
More Query from same tag
- Return an IQueryable that joins two tables
- LINQ SQL Attach, Update Check set to Never, but still Concurrency conflicts
- C# XML LINQ Syntax on Attributes
- Linq to sum on groups based on two columns
- How to get Database names from given Sql Server in LINQ
- C# LINQ group and count questions
- Order a list of string by quantity
- Is better Linq or SQL query for complex calculations and aggregations?
- LINQ: A better way to grouping a list of objects by multiple properties in to a list of lists?
- How would you implement LINQ methods with SelectMany?
- Checking if range of values pertaining to a single specified value is available in a List
- Set combobox with long datasource
- C# Linq query execution order
- What does "(n, index) => n >= index" mean in LINQ?
- Refactor to filtering lists using LINQ
- How to delete a single record from a list of records stored in a session
- English Dictionary word matching from a string
- C# Handle System.UnauthorizedAccessException in LINQ
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression."}
- How to populate my class object with linq to EF?
- Enum Condition in Linq Query in C#
- Linq Convert.ToInt32 in Query
- LINQ with anonymous class/properties
- LINQ query to return distinct field values from list of objects
- How to write a LINQ query to select from a collection with given set of matching IDs
- How can I run a query to systematically add up the results to a table or list?
- XDocument Linq Query to construct a dictionary
- ListView containing CheckBoxList - Selected items not showing as checked
- LINQ to entities Query to randomize row selection
- Order By - LINQ to Lambda