score:1
Make sure that in the constructor of Nov
you instantiate your enumeration.
public class Nov
{
public Nov()
{
Violations = Enumerable.Empty<Violation>();
}
public IEnumerable<Violation> Violations { get; }
}
So you know that there is always a reference to an empty enumeration of violations.
If it is a complex object or when the object is instantiated many times throughout the code or there are many different ways you can construct this object (you can also use the builder pattern here) you can also use a factory here the following way.
public class NovFactory
{
public INov Create()
{
return new Nov
{
Violations = Enumerable.Empty<Violation>();
}
}
}
public interface INov
{
IEnumerable<Violation> Violations { get; }
}
public class Nov : INov
{
public IEnumerable<Violation> Violations { get; set; }
}
score:1
This construction v.NOVId ?? default(int)
can be removed as there is no need to use default(int)
as you've already checked v.NOVId != null
:
var NOVIds = v.NOVId != null ?
new List<int> { v.NOVId }
: (from n in novs where n.Violations.Any(a => a.ViolationId == v.ViolationId)
select v.NOVId).ToList()
UPDATE:
To check whether a.ViolationId != null
and v.ViolationId != null
, you can use the following query:
var NOVIds = v.NOVId != null ?
new List<int> { v.NOVId }
: (from n in novs where n.Violations.Any(a =>
(a.ViolationId != null && v.ViolationId != null)
? (a.ViolationId == v.ViolationId) : false)
select v.NOVId).ToList()
UPDATE 1:
It is possible to use condition n.Violations != null
, to check whether n.Violations
is not null:
var NOVIds = v.NOVId != null ?
new List<int> { v.NOVId }
: (from n in novs where n.Violations != null && n.Violations.Any(a =>
(a.ViolationId != null && v.ViolationId != null)
? (a.ViolationId == v.ViolationId) : false)
select v.NOVId).ToList()
Source: stackoverflow.com
Related Query
- Linq to check before it creates select
- Check for null before select on linq
- Linq code to select one item
- Linq Select New List Property Null Check
- Check existence of a record before returning resultset in LINQ to SQL
- LINQ Source Code Available
- DataTable select with LiNQ and check is there duplicate rows or not
- MVC Controller: Using LINQ to check for duplicate value already existing in table before Save?
- Check if List is not null when using "contains" in LINQ query else select all records
- creating Linq to sqlite dbml from DbLinq source code
- Performance between check exists before add to list and distinct in linq
- Avoiding duplicate code in Linq Select method
- Given we only want the first record, is it better to sort before select in linq query?
- Check if column exists before execute select clause
- LINQ to entity query, check null before using navigation property
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Entity Framework Core - LINQ select over navigational properties creates invalid query
- How to Select top (5) contributors group by Business type code in c# linq query
- Linq to SQL check for null on object before accessing property
- source code for LINQ 101 samples
- Linq query null check in data source
- How to write aggregate query in LINQ reusing part of the select code
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- What is the linq select syntax to check for xml data with null elements?
- Hashset check count before enumerating using Linq
- Check if list is null before using linq
- How to write C# LINQ code to select based on condition
- Linq Select items where they are before a certain date or the first after
- check for null before getting property in linq to sql query
- LINQ Select mutiply columns as data source for combobox C#
More Query from same tag
- What would be the linq for this SQL Group Join
- How to create (predicate) for fetching data from DataTable using Express.call method
- problem with navigation properties in entity framework
- How does c# interpret my query?
- How to generate Expression<Func<T, TPaginatedKey>> expression based on a string?
- Get IDictionary(of String, IDictionary(of String, String)) using Linq
- Search an item in a complex object type list
- Xml updating with linq not working when quering
- DataTable Query
- Split a list by distinct date
- LINQ to XML with multiple elements
- How do I query identity data efficiently in ASP.Net Core?
- linq how to add symbol to empty lines from csv
- The type arguments cannot be inferred from the query
- Substring in linq
- Insert Anonymous object in a list of same anonymous type
- Invoking HashSet.Add(item) in Linq vs foreach loop
- LINQ To Entities - M to M selection returns more columns than needed
- linq distinct and select new query
- Post multible data from CheckBoxList
- DataGridView not binding to my linq queries
- Entity Framework – linq and null values
- Query many-to-many relationships with Entity Framework
- ERPTable columns being truncated
- what's wrong in this LINQ query
- Using First() with OrderBy and DynamicQuery in One-To-Many related tables
- LINQ query to return highest priced item in each category
- C# linq build table from sub list of list
- Group by Aggregate functions in LINQ
- How to get leaf node from a nested collection using lambda expression