score:0
you can get around this by always returning the anonymous where the anonymous object may have null properties.
using (var dbcontext = new datacontext())
{
var vals = dbcontext.primaries.select(p => new
{
name = p.name,
secondary = new { name = p.secondaryid.hasvalue ? p.secondary.name : null }
});
}
and if you really want to make secondary
null if p.secondaryid
is null you can add the following.
//tolist allows you to work with linq to objects rather than linq to entities.
//however, you'll generally want to call tolist() last for performance reasons.
var valscleaned = vals.tolist()
.select(v => {
if(v.secondary.name == null)
{
v.secondary == null;
}
return v;
});
score:0
i don't have a "solution" per se. to workaround this issue, i simply projected the entire secondary entity. i'm not happy with this solution.
using (var dbcontext = new datacontext())
{
var vals = dbcontext.primaries.select(p => new
{
name = p.name,
secondary = p.secondary
});
}
obviously, projecting an entire entity is akin to "select *" - which is a bad practice. and it may not work for you, depending on your actual query.
Source: stackoverflow.com
Related Query
- Selecting null in a select select clause in EF5
- Select clause null
- Select statement to prevent selecting null
- where Clause in Asp.net is returning null exception in the code below
- Linq code to select one item
- Filtering Null values in Select
- In LINQ, select all values of property X where X != null
- How to select array index after Where clause using Linq?
- LINQ: adding where clause only when a value is not null
- lambda expression join multiple tables with select and where clause
- How to select last record in a LINQ GroupBy clause
- linq where clause and count result in null exception
- A query body must end with a select clause or a group clause why is here an error?
- Why C# LINQ expressions must end with Select or Group By Clause where as no such restriction in VB.Net
- Linq Select New List Property Null Check
- selecting a property from FirstOrDefault in case FirstOrDefault returns null
- Using Function in Select Clause of Entity Framework Query
- Linq query works with null but not int? in where clause
- SELECT AS in Linq with WHERE clause
- null reference exception with linq .where clause
- Linq where clause with multiple conditions and null check
- How to Use Effeciently Where Clause or Select in LINQ Parallel in Large Dataset
- Linq to SQL: Where clause comparing a Nullable<DateTime> with a SQL datetime null column
- LINQ Source Code Available
- Select clause containing non-EF method calls
- SELECT NEW with a potentially null field using LINQ and Entity Framework
- Linq with where clause in many-to-many EF Code First object
- .NET 4 Code Contracts: "requires unproven: source != null"
- EF Code First comparing null values generates strange query
- When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?
More Query from same tag
- Selecting first 10 records, then next 10, paging using Linq
- Convert EntityModel to DTO Using "Basic" Custom Method
- How can I set a zero-value instead of null in LINQ Select Sum
- LINQ to XML query and filtering names
- Filter a certain value in List according to Where using LINQ
- Is there is a C# function which is similar to .Map() in JavaScript?
- System.Linq.IQueryable Error
- Converting a lambda expression into a unique key for caching
- Extract a method from two, almost identical, Linq queries
- Compare two lists via LINQ in c#?
- Linq To SQL Delete + Insert best practice
- Merge two c# object(json) lists as parent child
- Very Simple xml to linq query
- DefaultIfEmpty Exception "bug or limitation" with EF Core
- Error creating a Linq query
- LINQ and Dictionary Thread Safety
- using PredicateBuilder inside a select
- Select top 3 by Take() in Linq
- Linq complex select
- List<Base> contains different Derived types. How to convert safely with LINQ?
- Why I get another reference each time I call IEnumerable.Last()
- Using dictionary keys as field name in LINQ
- return custom json from LINQ select statement
- LINQ to SQL execute method on select
- Linq group by one multiple properties
- LINQ query with value from dropdown to compare with data
- Complex WHERE clause on an array in Azure DocumentDb
- Change Connection String from Application B for Application A
- Linq static method error
- Get index of first list and use it in another list