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
- Loop through list and compare previous line to current line. I have a list of account numbers mostly duplicates
- Linq Groupby return original object
- compare two list and return not matching items using linq
- Strange LINQ query error
- Dynamic query using LINQ to SQL
- linq Command not getting the answer for DB
- Simpliest way to transform a Dictionary<int, Dictionary<int, Foo> > to IEnumerable<IEnumerable<Foo> > in Linq?
- Grouping data by date ranges
- Using Linq, how do I check that a booking does not conflict with an existing booking?
- Use orderby using linq query
- Filter one list using index value obtained from another list using linq
- Matching on search attributes selected by customer on front end
- How to get a running total of a decimal in a query?
- Group DataTable by column and convert to List<DataTable>
- Create GroupBy Statments Dynamically By Strings Array - Linq to Entity
- Linq Sorting on list and its nested collection of same class
- How to group product by category on list view by LinQ?
- Getting Table's Row Index Using LINQ
- Get Just 3rd level XML nodes' names LINQ C#
- How to use the IEqualityComparer
- LINQ without using cycle
- Entity Framework Query for Complex Sql
- Microsoft Dynamics 2011 N:N LINQ query with where clause containing Guid
- Sql Query Convertion into Linq Query In Wcf Servive
- Linq case statement on boolean field depending on int value
- Using XElement to parse this XML into an array
- Why can't I use a where clause on both tables in a LINQ Group Join?
- LINQ - update a field within where clause
- Is a linq query to ConcurrentDictionary Values threadsafe?
- Linq Parsing Error when trying to create seperation of concerns