score:6
Accepted answer
you are right about the projection.
if contact
has a navigation property forms
you can project:
from c in context.contacts
select new { contact = c, formscount = c.forms.count() }
if not, you'll have to use a subquery:
from c in context.contacts
select new
{
contact = c,
formscount = context.forms.count(f => f.contact_id == c.id)
}
ef will handle both situations in one sql query.
Source: stackoverflow.com
Related Query
- SQL subquery result in LINQ and Entity Framework Code First
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- Linq to SQL and Entity Framework differences?
- difference between linq to sql and entity framework
- LINQ: Translating a SQL WITH clause to LINQ and Entity Framework
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- Linq to SQL and Entity Framework in Same project
- Entity Framework - Linq : how query to take 2 first ordered and grouped list
- How to filter related data using Entity Framework and LINQ to SQL and LinqKit PredicateBuilder Or IdeaBlade DevForce
- Entity Framework Code First ToList method timing out on SQL Azure
- Entity Framework Code First override onModelCreating() for TPT Inheritance Screwing Up Identity User and Role Models
- SQL to LINQ for SubQuery Entity Framework
- How can I convert Sql query to Linq and its equivalent with Join() method in Entity Framework Core
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- LINQ - Entity framework code first - Grouped results to custom class
- Entity Framework 4 and Linq to Entities specifications: How to code it?
- StartsWith with any string from list to SQL request with LINQ and Entity Framework
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- c# WPF bind combobox to TPH in Entity Framework code first using LINQ
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Code First Entity Framework Linq Statement Returning Missing Records
- Translating datediff LINQ query to SQL with Entity Framework Core and .NET Core 3.1
- Entity Framework Code Most First Efficient Linq Query
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- XDocument insert XML data into SQL using Linq and Entity Framework
- Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query
- How to return just first parent element, but many children and grandchildren in Linq with Entity Framework - SYBASE ASE Connector bug
- LINQ to SQL together with Entity Framework Database first
- Reuse stored procedure result in linq to sql in Code First
- Entity Framework, Code First and Full Text Search
More Query from same tag
- How to get pair value from CultureTypes object in C#?
- Error converting LINQ anonymous type to IList<>
- How to remove from the list the items that have parents in the same list
- how to list all nodes of same type in xml using linq?
- Entity Framework: Select all entities whose sub entities have a certain flag set
- StreamInsight 2.1 adapter to observer
- Linq Query reuse
- Get top n rows and sum the rest and call it others in Entity Framework linq lambda query
- LINQ: How to perform an update like UPDATE <T> SET Update(<T>, <T>) SELECT FROM clause in LINQ?
- linq with dataTable in c#
- Using LINQ Join Extension-Method on List & DataRowCollection
- OrderBy on List<string> property
- How to use Linq as an IF - Condition
- Simple question regarding LINQ
- Retrieve records from the database matching multiple values of a list
- Linq: get a custom class joining to tables
- What can be causing Html.ValidateFor() method to produce a compile error?
- Linq order by boolean
- Linq to query with multiple row subquery (without join)
- How to do a 'composite' select using LINQ?
- How do I prevent inserting new records into tables while using Linq and SQL?
- Linq ToDictionary will not implicitly convert class to interface
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- LINQ won't sum a group with a float? property
- Dynamic linq - Group by interval (DateTime, Numeric)
- Serialize a class containing a list<float[]> using SOAP Edit: list<MyClass>
- Linq count returns 0 but foeach returns something else
- Making a self-contained boolean Expression with PredicateBuilder
- LINQ return value conversion failed from string to int
- converting a string to an acceptable SQL value