Entity Framework EF Query using LINQ for related Entities - Getting Customers with their orders from specific...











up vote
0
down vote

favorite













This question already has an answer here:




  • EF: Include with where clause

    2 answers




I am having an issue with LINQ query for getting a Customers list from Database with orders from only specific period of time. I Tried many different things but still can't figure it out how such LINQ query schould look like. Those enitites looks something like this:



public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}

public class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
public Customer Customer { get; set; }
}


Ammong many different thing i tried this:



var customers = context.Customers
.Include(o => o.Orders.Where(a=>a.DateTime > start && a.DateTime < end))
.ToList();


, but i get an System.InvalidOperationException.
I guess .Include can't be used that way.



PS: I am a newbie in EF and LINQ so please don't be harsh.



EDIT:
I think i have to clarifiy what i am trying to do:



After reciving a query from an user, who gives a period of time for example: from 01.01.2015 to 01.01.2018



The database is supposed to return ALL CUSTOMERS with realted orders but ONLY FROM that specific period of time. So I could for example invoke .Count() method and check how many orders a particular customer has placed during this period of time.










share|improve this question















marked as duplicate by Gert Arnold c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 18:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Have you tried with the Where out of parenthesis.
    – Llazar
    Nov 11 at 18:12










  • var customers = context.Customers .Include(o => o.Orders) .Where(a => a.DateTime > start && a.DateTime < end) .ToList(); Something like this? Yeah,it doesn't work, "Customer doesn't contain a definition for a "DateTime" [...] "
    – User
    Nov 11 at 18:16






  • 1




    The property you're using to filter called Date not DateTime.. This is why it says " Customer doesn't contain a definition for a "DateTime" "
    – Ahmad Ibrahim
    Nov 11 at 18:29












  • No, it's not the issue. Thats how i just typed it here(with mistake), but in code it's DateTime. The issue is how to iterate with linq through Orders list realted to specific Customers and get Only Orders from specific Period of time.
    – User
    Nov 11 at 18:36










  • As you see, this question is being asked more often. The duplicate is just one appearance. Filtered Include is still under discussion.
    – Gert Arnold
    Nov 11 at 18:51















up vote
0
down vote

favorite













This question already has an answer here:




  • EF: Include with where clause

    2 answers




I am having an issue with LINQ query for getting a Customers list from Database with orders from only specific period of time. I Tried many different things but still can't figure it out how such LINQ query schould look like. Those enitites looks something like this:



public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}

public class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
public Customer Customer { get; set; }
}


Ammong many different thing i tried this:



var customers = context.Customers
.Include(o => o.Orders.Where(a=>a.DateTime > start && a.DateTime < end))
.ToList();


, but i get an System.InvalidOperationException.
I guess .Include can't be used that way.



PS: I am a newbie in EF and LINQ so please don't be harsh.



EDIT:
I think i have to clarifiy what i am trying to do:



After reciving a query from an user, who gives a period of time for example: from 01.01.2015 to 01.01.2018



The database is supposed to return ALL CUSTOMERS with realted orders but ONLY FROM that specific period of time. So I could for example invoke .Count() method and check how many orders a particular customer has placed during this period of time.










share|improve this question















marked as duplicate by Gert Arnold c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 18:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Have you tried with the Where out of parenthesis.
    – Llazar
    Nov 11 at 18:12










  • var customers = context.Customers .Include(o => o.Orders) .Where(a => a.DateTime > start && a.DateTime < end) .ToList(); Something like this? Yeah,it doesn't work, "Customer doesn't contain a definition for a "DateTime" [...] "
    – User
    Nov 11 at 18:16






  • 1




    The property you're using to filter called Date not DateTime.. This is why it says " Customer doesn't contain a definition for a "DateTime" "
    – Ahmad Ibrahim
    Nov 11 at 18:29












  • No, it's not the issue. Thats how i just typed it here(with mistake), but in code it's DateTime. The issue is how to iterate with linq through Orders list realted to specific Customers and get Only Orders from specific Period of time.
    – User
    Nov 11 at 18:36










  • As you see, this question is being asked more often. The duplicate is just one appearance. Filtered Include is still under discussion.
    – Gert Arnold
    Nov 11 at 18:51













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • EF: Include with where clause

    2 answers




I am having an issue with LINQ query for getting a Customers list from Database with orders from only specific period of time. I Tried many different things but still can't figure it out how such LINQ query schould look like. Those enitites looks something like this:



public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}

public class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
public Customer Customer { get; set; }
}


Ammong many different thing i tried this:



var customers = context.Customers
.Include(o => o.Orders.Where(a=>a.DateTime > start && a.DateTime < end))
.ToList();


, but i get an System.InvalidOperationException.
I guess .Include can't be used that way.



PS: I am a newbie in EF and LINQ so please don't be harsh.



EDIT:
I think i have to clarifiy what i am trying to do:



After reciving a query from an user, who gives a period of time for example: from 01.01.2015 to 01.01.2018



The database is supposed to return ALL CUSTOMERS with realted orders but ONLY FROM that specific period of time. So I could for example invoke .Count() method and check how many orders a particular customer has placed during this period of time.










share|improve this question
















This question already has an answer here:




  • EF: Include with where clause

    2 answers




I am having an issue with LINQ query for getting a Customers list from Database with orders from only specific period of time. I Tried many different things but still can't figure it out how such LINQ query schould look like. Those enitites looks something like this:



public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}

public class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
public Customer Customer { get; set; }
}


Ammong many different thing i tried this:



var customers = context.Customers
.Include(o => o.Orders.Where(a=>a.DateTime > start && a.DateTime < end))
.ToList();


, but i get an System.InvalidOperationException.
I guess .Include can't be used that way.



PS: I am a newbie in EF and LINQ so please don't be harsh.



EDIT:
I think i have to clarifiy what i am trying to do:



After reciving a query from an user, who gives a period of time for example: from 01.01.2015 to 01.01.2018



The database is supposed to return ALL CUSTOMERS with realted orders but ONLY FROM that specific period of time. So I could for example invoke .Count() method and check how many orders a particular customer has placed during this period of time.





This question already has an answer here:




  • EF: Include with where clause

    2 answers








c# sql .net entity-framework linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 19:06

























asked Nov 11 at 18:05









User

11




11




marked as duplicate by Gert Arnold c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 18:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Gert Arnold c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 18:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Have you tried with the Where out of parenthesis.
    – Llazar
    Nov 11 at 18:12










  • var customers = context.Customers .Include(o => o.Orders) .Where(a => a.DateTime > start && a.DateTime < end) .ToList(); Something like this? Yeah,it doesn't work, "Customer doesn't contain a definition for a "DateTime" [...] "
    – User
    Nov 11 at 18:16






  • 1




    The property you're using to filter called Date not DateTime.. This is why it says " Customer doesn't contain a definition for a "DateTime" "
    – Ahmad Ibrahim
    Nov 11 at 18:29












  • No, it's not the issue. Thats how i just typed it here(with mistake), but in code it's DateTime. The issue is how to iterate with linq through Orders list realted to specific Customers and get Only Orders from specific Period of time.
    – User
    Nov 11 at 18:36










  • As you see, this question is being asked more often. The duplicate is just one appearance. Filtered Include is still under discussion.
    – Gert Arnold
    Nov 11 at 18:51


















  • Have you tried with the Where out of parenthesis.
    – Llazar
    Nov 11 at 18:12










  • var customers = context.Customers .Include(o => o.Orders) .Where(a => a.DateTime > start && a.DateTime < end) .ToList(); Something like this? Yeah,it doesn't work, "Customer doesn't contain a definition for a "DateTime" [...] "
    – User
    Nov 11 at 18:16






  • 1




    The property you're using to filter called Date not DateTime.. This is why it says " Customer doesn't contain a definition for a "DateTime" "
    – Ahmad Ibrahim
    Nov 11 at 18:29












  • No, it's not the issue. Thats how i just typed it here(with mistake), but in code it's DateTime. The issue is how to iterate with linq through Orders list realted to specific Customers and get Only Orders from specific Period of time.
    – User
    Nov 11 at 18:36










  • As you see, this question is being asked more often. The duplicate is just one appearance. Filtered Include is still under discussion.
    – Gert Arnold
    Nov 11 at 18:51
















Have you tried with the Where out of parenthesis.
– Llazar
Nov 11 at 18:12




Have you tried with the Where out of parenthesis.
– Llazar
Nov 11 at 18:12












var customers = context.Customers .Include(o => o.Orders) .Where(a => a.DateTime > start && a.DateTime < end) .ToList(); Something like this? Yeah,it doesn't work, "Customer doesn't contain a definition for a "DateTime" [...] "
– User
Nov 11 at 18:16




var customers = context.Customers .Include(o => o.Orders) .Where(a => a.DateTime > start && a.DateTime < end) .ToList(); Something like this? Yeah,it doesn't work, "Customer doesn't contain a definition for a "DateTime" [...] "
– User
Nov 11 at 18:16




1




1




The property you're using to filter called Date not DateTime.. This is why it says " Customer doesn't contain a definition for a "DateTime" "
– Ahmad Ibrahim
Nov 11 at 18:29






The property you're using to filter called Date not DateTime.. This is why it says " Customer doesn't contain a definition for a "DateTime" "
– Ahmad Ibrahim
Nov 11 at 18:29














No, it's not the issue. Thats how i just typed it here(with mistake), but in code it's DateTime. The issue is how to iterate with linq through Orders list realted to specific Customers and get Only Orders from specific Period of time.
– User
Nov 11 at 18:36




No, it's not the issue. Thats how i just typed it here(with mistake), but in code it's DateTime. The issue is how to iterate with linq through Orders list realted to specific Customers and get Only Orders from specific Period of time.
– User
Nov 11 at 18:36












As you see, this question is being asked more often. The duplicate is just one appearance. Filtered Include is still under discussion.
– Gert Arnold
Nov 11 at 18:51




As you see, this question is being asked more often. The duplicate is just one appearance. Filtered Include is still under discussion.
– Gert Arnold
Nov 11 at 18:51












1 Answer
1






active

oldest

votes

















up vote
0
down vote













This should give a list of customers who have at least one order between start and end date.



var customers = context.Customers
.Where(o => o.Orders.Any(a=>a.Date > start && a.Date < end))
.ToList();


If you need customers who have all orders between start and end date, you can use All instead of Any.



var customers = context.Customers
.Where(o => o.Orders.All(a=>a.Date > start && a.Date < end))
.ToList();





share|improve this answer





















  • I need a list of all customers, but with only orders from that given period of time. For example Customer "1" has 2 orders 1- Date : 01.01.2015 2-Date: 01.01.2017 An user gives a period of time from 01.01.2016 to 01.01.2018 He should recive and Customer 1 entity but only with 1 Order included -> Order nr.2
    – User
    Nov 11 at 18:52




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













This should give a list of customers who have at least one order between start and end date.



var customers = context.Customers
.Where(o => o.Orders.Any(a=>a.Date > start && a.Date < end))
.ToList();


If you need customers who have all orders between start and end date, you can use All instead of Any.



var customers = context.Customers
.Where(o => o.Orders.All(a=>a.Date > start && a.Date < end))
.ToList();





share|improve this answer





















  • I need a list of all customers, but with only orders from that given period of time. For example Customer "1" has 2 orders 1- Date : 01.01.2015 2-Date: 01.01.2017 An user gives a period of time from 01.01.2016 to 01.01.2018 He should recive and Customer 1 entity but only with 1 Order included -> Order nr.2
    – User
    Nov 11 at 18:52

















up vote
0
down vote













This should give a list of customers who have at least one order between start and end date.



var customers = context.Customers
.Where(o => o.Orders.Any(a=>a.Date > start && a.Date < end))
.ToList();


If you need customers who have all orders between start and end date, you can use All instead of Any.



var customers = context.Customers
.Where(o => o.Orders.All(a=>a.Date > start && a.Date < end))
.ToList();





share|improve this answer





















  • I need a list of all customers, but with only orders from that given period of time. For example Customer "1" has 2 orders 1- Date : 01.01.2015 2-Date: 01.01.2017 An user gives a period of time from 01.01.2016 to 01.01.2018 He should recive and Customer 1 entity but only with 1 Order included -> Order nr.2
    – User
    Nov 11 at 18:52















up vote
0
down vote










up vote
0
down vote









This should give a list of customers who have at least one order between start and end date.



var customers = context.Customers
.Where(o => o.Orders.Any(a=>a.Date > start && a.Date < end))
.ToList();


If you need customers who have all orders between start and end date, you can use All instead of Any.



var customers = context.Customers
.Where(o => o.Orders.All(a=>a.Date > start && a.Date < end))
.ToList();





share|improve this answer












This should give a list of customers who have at least one order between start and end date.



var customers = context.Customers
.Where(o => o.Orders.Any(a=>a.Date > start && a.Date < end))
.ToList();


If you need customers who have all orders between start and end date, you can use All instead of Any.



var customers = context.Customers
.Where(o => o.Orders.All(a=>a.Date > start && a.Date < end))
.ToList();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 18:36









Ahmad Ibrahim

1,65421121




1,65421121












  • I need a list of all customers, but with only orders from that given period of time. For example Customer "1" has 2 orders 1- Date : 01.01.2015 2-Date: 01.01.2017 An user gives a period of time from 01.01.2016 to 01.01.2018 He should recive and Customer 1 entity but only with 1 Order included -> Order nr.2
    – User
    Nov 11 at 18:52




















  • I need a list of all customers, but with only orders from that given period of time. For example Customer "1" has 2 orders 1- Date : 01.01.2015 2-Date: 01.01.2017 An user gives a period of time from 01.01.2016 to 01.01.2018 He should recive and Customer 1 entity but only with 1 Order included -> Order nr.2
    – User
    Nov 11 at 18:52


















I need a list of all customers, but with only orders from that given period of time. For example Customer "1" has 2 orders 1- Date : 01.01.2015 2-Date: 01.01.2017 An user gives a period of time from 01.01.2016 to 01.01.2018 He should recive and Customer 1 entity but only with 1 Order included -> Order nr.2
– User
Nov 11 at 18:52






I need a list of all customers, but with only orders from that given period of time. For example Customer "1" has 2 orders 1- Date : 01.01.2015 2-Date: 01.01.2017 An user gives a period of time from 01.01.2016 to 01.01.2018 He should recive and Customer 1 entity but only with 1 Order included -> Order nr.2
– User
Nov 11 at 18:52





Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues