score:69
i figured it out, thanks for the suggestions anyway. the solution is to do this (2nd attempt in my question):
var qry = (from a in actions
join u in users on a.userid equals u.userid
select a).include("user")
the reason intellisense didn't show include after the query was because i needed the following using:
using system.data.entity;
everything worked fine doing this.
score:0
i use for this the loadwith option
var dataoptions = new system.data.linq.dataloadoptions();
dataoptions.loadwith<action>(ac => as.user);
ctx.loadoptions = dataoptions;
thats it. ctx is your datacontext. this works for me :-)
score:2
doing the basic query mentioned in your posted question you won't be able to see the user properties unless you return an anonymous type as following:
from a in actions
join u in users on a.userid equals u.userid
select new
{
actionuserid = a.userid
.
.
.
userproperty1 = u.userid
};
however to use the include method on the objectcontext you could use the following:
make sure you have lazyloading off by using the following line:
entities.contextoptions.lazyloadingenabled = false;
then proceed by
var bar = entities.actions.include("user");
var foo = (from a in bar
select a);
score:17
if what you want is a query that will return all action
entities whose associated user
entity actually exists via the action.userid
foreign key property, this will do it:
var results = context.actions
.include("user")
.where(action =>
context.users.any(user =>
user.userid == action.userid));
however you don't have to use foreign key properties in order to do filtering, since you also have navigation properties. so your query can be simplified by filtering on the action.user
navigation property instead, like in this example:
var results = context.actions
.include("user")
.where(action => action.user != null);
if your model states that the action.user
property can never be null (i.e. the action.userid
foreign key is not nullable in the database) and what you want is actually all action
entities with their associated users
, then the query becomes even simpler
var results = context.actions.include("user");
score:21
better, refactor friendly code (ef6)
using system.data.entity;
[...]
var x = (from cart in context.shoppingcarts
where table.id == 123
select cart).include(t => t.cartitems);
or
var x = from cart in context.shoppingcarts.include(nameof(shoppingcart.cartitems))
where table.id == 123
select cart;
update 3/31/2017
you can also use include in lambda syntax for either method:
var x = from cart in context.shoppingcarts.include(p => p.shoppingcart.cartitems))
where table.id == 123
select cart;
Source: stackoverflow.com
Related Query
- How does LINQ expression syntax work with Include() for eager loading
- How to reuse a linq expression for 'Where' when using multiple source tables
- How does linq Expression<TDelegate> assignment work on a language syntax level
- How can I use Linq expression for Join with GroupBy in EF Core 3.1
- How does cast in C#/.NET 3.5 work for types with '?'
- How to convert Linq expression with multiple joins into method syntax OR retrieve select index?
- How does Linq projection work between Extention Method and query expression
- How to catch syntax node for identifier in LINQ expression group ... into <identifier>
- Saved projection expression for re-use in different linq expressions with two source objects
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- How to flatten nested objects with linq expression
- Does LINQ work with IEnumerable?
- What does this C# code with an "arrow" mean and how is it called?
- Linq for NHibernate and fetch mode of eager loading
- Linq syntax for OrderBy with custom Comparer<T>
- How do I create a Linq expression tree with an F# lambda?
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- The Include path expression must refer to a navigation property defined on the type.in eager loading
- C# 6 null conditional operator does not work for LINQ query
- C# re-use LINQ expression for different properties with same type
- Order by does not work with Concat() in LINQ
- Disable all lazy loading or force eager loading for a LINQ context
- How much overhead does 'Update Check' have for LINQ UPDATES
- how does except method work in linq
- How does OrderBy work with regard to strings in C#?
- How does Linq work (behind the scenes)?
- How to implement SkipWhile with Linq to Sql without first loading the whole list into memory?
- FirstorDefault() causes lazy loading or eager loading for linq to sql
- How to get a value out of a Span<T> with Linq expression trees?
- How does a LINQ expression know that Where() comes before Select()?
More Query from same tag
- Rearrange of a nested List with Linq
- Storing LINQ Query object to an Integer
- Trying to Add a value as CSV in Linq lambda statement with groupby
- Linq to entities lazy loading
- Get only parent class data entity framework
- List.Where passing in object of different type (Predicates)
- Unable to process loop faster
- Linq2SQl eager load with multiple DataLoadOptions
- Using Any to check against a list in NHibernate
- Object reference not set to an instance of an object with no NULL fields present
- How to find the maximum value for each key in a List of Dictionaries using LINQ?
- Selecting Distinct Items within Array using PowerShell and Linq
- How to get name of a RavenDB document
- How to Merge the List in Linq Query
- Equivalent of LINQ in Java 8
- EF Union issue "cannot convert from <AnonymusType#1 to AnonymusType#2>
- Conversion from Hibernate to Entity Framework 6
- Join large list of Integers into LINQ Query
- How to select name corresponding the id using LINQ
- Why is Linq significantly slower?
- My lack of understanding LINQ
- How to create IEnumerable.ToString
- How to add index to async LINQ query
- How to access many to many mapped table in c# with Entity Framework
- Linq Parent Child Query to SQL Server
- Reflection in Linq
- How to get repeated values in LINQ
- LINQ sum and groupby and return empty records?
- aggregation count in linq query returns null
- LINQ nested query issue - System.NotSupportedException