score:0
try following. i models you database _context as a class just so i could test the syntax. remember that one customer may map to more than one transaction. you may want to use groupby id so you get 10 different customers.
class program
{
static void main(string[] args)
{
context _context = new context();
var ids = (from c in _context.customers
join t in _context.transactions on c.id equals t.customerid
select new { c = c, t = t})
.orderbydescending(x => x.c.created)
.take(10)
.tolist();
}
}
public class context
{
public list<customer> customers { get; set; }
public list<transaction> transactions { get; set; }
}
public class customer
{
public guid id { get; set; }
public string name { get; set; }
public datetime created { get; set; }
public hashset<transaction> transactions { get; set; }
}
public class transaction
{
public guid id { get; set; }
public decimal amount { get; set; }
public datetime created { get; set; }
public guid customerid { get; set; }
public customer customer { get; set; }
}
you may want to try this instead :
var ids = (from c in _context.customers
join t in _context.transactions on c.id equals t.customerid
select new { c = c, t = t})
.orderbydescending(x => x.c.created)
.groupby(x => x.c.id)
.selectmany(x => x.take(10))
.tolist();
eliminating the join will speed up results. you always can get the customer info in another query.
var transactions = _context.transactions
.orderbydescending(x => x.created)
.groupby(x => x.customerid)
.select(x => x.take(10))
.tolist();
score:0
try this:
var customers = customerservice.getall().orderbydescending(c => c.created).take(10).tolist().asqueryable();
var transactions = transactionservice.getall().orderbydescending(t => t.created).take(10).tolist().asqueryable();
transactions = transactions.where(t => customers.any(c => c.customerid == t.id));
score:2
you wrote:
i wanted to grab the 10 latest transactions and 10 latest customers in one linq query
it is a bit unclear what you want. i doubt that you want one sequence with a mix of customers and transactions. i guess that you want the 10 newest customers, each with their last 10 transactions?
i wonder why you would deviate from the entity framework code first conventions. if your class customer represents a row in your database, then surely it doesn't have a hashset<transaction>
?
a one-to-many of a customer
with his transactions
should be modeled as follows:
class customer
{
public int id {get; set;}
... // other properties
// every customer has zero or more transactions (one-to-many)
public virtual icollection<transaction> transactions {get; set;}
}
class transaction
{
public int id {get; set;}
... // other properties
// every transaction belongs to exactly one customer, using foreign key
public int customerid {get; set;}
public virtual customer customer {get; set;}
}
public mydbcontext : dbcontext
{
public dbset<customer> customers {get; set;}
public dbset<transaction> transactions {get; set;}
}
this is all that entity framework needs to know to detect the tables you want to create, to detect your one-to-many relationship, and to detect the primary keys and foreign keys. only if you want different names of tables or columns, you'll need attributes and/or fluent api
the major differences between my classes and yours, is that the one-to-many relation is represented by virtual properties. the hashset is an icollection. after all, your transactions
table is a collection of rows, not a hashset
in entity framework the columns of your tables are represented by non-virtual properties; the virtual properties represent the relations between the tables (one-to-many, many-to-many, ...)
quite a lot of people tend to (group-)join tables, when they are using entity framework. however, life is much easier if you use the virtual properties
back to your question
i want (some properties of) the 10 newest customers, each with (several properties of) their 10 latest transactions
var query = dbcontext.customers // from the collection of customer
.orderbydescending(customer => customer.created) // order this by descending creation date
.select(customer => new // from every customer select the
{ // following properties
// select only the properties you actually plan to use
id = customer.id,
created = customer.created,
name = customer.name,
...
latesttransactions = customer.transactions // order the customer's collection
.orderby(transaction => transaction.created) // of transactions
.select(transaction => new // and select the properties
{
// again: select only the properties you plan to use
id = transaction.id,
created = transaction.created,
...
// not needed you know it equals customer.id
// customerid = transaction.customerid,
})
.take(10) // take only the first 10 transactions
.tolist(),
})
.take(10); // take only the first 10 customers
entity framework knows the one-to-many relationship and recognizes that a group-join is needed for this.
one of the slower parts of your query is the transfer of the selected data from the dbms to your local process. hence it is wise to limit the selected data to the data you actually plan to use. if customer with id 4 has 1000 transactions, it would be a waste to transfer the foreign key for every transaction, because you know it has value 4.
if you really want to do the join yourself:
var query = dbcontext.customers // groupjoin customers and transactions
.groupjoin(dbcontext.transactions,
customer => customer.id, // from each customer take the primary key
transaction => transaction.customerid, // from each transaction take the foreign key
(customer, transactions) => new // take the customer with his matching transactions
{ // to make a new:
id = customer.id,
created = customer.created,
...
latesttransactions = transactions
.orderby(transaction => transaction.created)
.select(transaction => new
{
id = transaction.id,
created = transaction.created,
...
})
.take(10)
.tolist(),
})
.take(10);
Source: stackoverflow.com
Related Query
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- Select data from multiple unrelated tables with LINQ to Entity Framework
- How to bind data from mutiple tables to datagridview using Entity Framework and use CRUD operations?
- Retrieving Data From Multiple Tables in a LINQ to SQL query
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- Returning data from multiple sql tables entity framework web api
- How to filter related data using Entity Framework and LINQ to SQL and LinqKit PredicateBuilder Or IdeaBlade DevForce
- Searching for multiple strings using single database query with entity framework and LINQ
- Joining or Including multiple tables with Entity Framework Core LINQ to Entities - One to Many and then Many to One
- How to design nested query for fetching data from tables of MS SQL using Entity Framework?
- One LINQ query to get counts from several entities when using entity framework core
- linq query to get dependent data in one connection using entity framework 6
- How to retrieve data from multiple tables using Entity Framework
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Search multiple tables with no relation by special value with one query using linq to entity
- How to select from multiple tables using LINQ using a union query to return only one column?
- convert SQL query with multiple join on multiple tables using group by on multiple columns and with aggregate function into LINQ
- Sql Join between two tables using Entity framework - Trying to bind Gridview data from two tables
- XDocument insert XML data into SQL using Linq and Entity Framework
- Take the first five elements and the last five elements from an array by one query using LINQ
- Deleting multiple records with Entity Framework using a single LINQ query
- Dynamic linq query expression tree for sql IN clause using Entity framework
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- Entity Framework Linq Query to List - Error when using contains: Only primitive types, enumeration types and entity types are supported
- linq query to join two tables and get the count from one table values from the other
- LinQ query with multiple tables and extracting data
- Query Xml from SQL using Entity Framework Database First
- LINQ to SQL join 3 tables and select multiple columns and also using Sum
- How to query many-to-many relationship with 'AND' condition using LINQ and Entity Framework
- Retrieve and print data from dynamic sql query with linq
More Query from same tag
- How do i combine 2 Linq queries into 1?
- Iterate through class properties using an action?
- Linq to XML: parsing the data but not fetching it
- LINQ index of min value of one Property of a Custom Object c#
- Filtering A C# LINQ Statement With A Switch Statement
- For Each in LINQ Select
- Help Translating SQL to LINQ-to-Entities
- How do I LINQ nested lists to JSON in Web Api 2?
- Combine List of like DataTables
- Sequence contains no elements even though I add OrDefault
- How do I return all order items for all orders?
- How do I do the following Linq / Lambda code?
- LINQ + CSV: How do i split a CSV file by its headers in C#
- Linq OrderBy calculated property
- How to access a table in linq
- Modifying Lambda Expression
- LINQ Enumerable in Enumerable - Why?
- Linq order by Seq Number but also have records with deleted flag ordered last
- How to select only not null objects from list
- How to merge a collection of collections in Linq
- Conditional Linq Select on Nested object
- ef cf linq populate ignored property
- I have never used JOIN. When is proper time?
- For loop to linq
- How can I code a Linq query to do an upward Include?
- C#/LINQ: How to Query this XML Structure
- Convert SQL to lambda
- How can two left joins from a single table on similar fields be translated into LINQ when more than two tables are involved?
- How to pivot data in LINQ without hard coding columns
- Populating a C# / XAML combobox using EF and LINQ