Filtering Related Entites with Entity Framework
According to this StackOverflow answer:
Linq to Entities - how to filter on child entities
you should be able to filter down the list of related entities in Entity Framework by utilizing a projection, like I've done here:
Company company = _context.Company
.Where(g => g.CompanyId == id)
.Select(comp => new
group = comp,
operators = comp.Operator,
formFamilies = comp.FormFamily.Where(ff => ff.IsActive ?? false)
).AsEnumerable().Select(i => i.group).FirstOrDefault();
To give a quick overview of what I'm trying to obtain here, I'm trying to get a list of all of the active form families associated with this company object, however, whenever I restrict the results in any way, the result set is empty.
- If the line were
formFamilies = comp.FormFamily
then it returns two results, one active one inactive - If the line is
formFamilies = comp.FormFamily.Where(ff => true)
then it returns nothing - If the line is
formFamilies = comp.FormFamily.OrderBy(ff => ff.FormFamilyId)
then it returns nothing.
Any sort of modification that I do to comp.FormFamily
means the result set returns nothing, I've dug through the deepest sections of SA to try to find a solution, and tried every solution I've found, but nothing seems to cause this list to return anything.
c# asp.net entity-framework linq-to-entities
add a comment |
According to this StackOverflow answer:
Linq to Entities - how to filter on child entities
you should be able to filter down the list of related entities in Entity Framework by utilizing a projection, like I've done here:
Company company = _context.Company
.Where(g => g.CompanyId == id)
.Select(comp => new
group = comp,
operators = comp.Operator,
formFamilies = comp.FormFamily.Where(ff => ff.IsActive ?? false)
).AsEnumerable().Select(i => i.group).FirstOrDefault();
To give a quick overview of what I'm trying to obtain here, I'm trying to get a list of all of the active form families associated with this company object, however, whenever I restrict the results in any way, the result set is empty.
- If the line were
formFamilies = comp.FormFamily
then it returns two results, one active one inactive - If the line is
formFamilies = comp.FormFamily.Where(ff => true)
then it returns nothing - If the line is
formFamilies = comp.FormFamily.OrderBy(ff => ff.FormFamilyId)
then it returns nothing.
Any sort of modification that I do to comp.FormFamily
means the result set returns nothing, I've dug through the deepest sections of SA to try to find a solution, and tried every solution I've found, but nothing seems to cause this list to return anything.
c# asp.net entity-framework linq-to-entities
could u plz add yourCompany
andFormFamily
entity to your question?
– er-shoaib
Nov 12 at 5:36
It would be awesome if you could provide a Minimal, Complete, and Verifiable example
– JohnB
Nov 12 at 5:44
add a comment |
According to this StackOverflow answer:
Linq to Entities - how to filter on child entities
you should be able to filter down the list of related entities in Entity Framework by utilizing a projection, like I've done here:
Company company = _context.Company
.Where(g => g.CompanyId == id)
.Select(comp => new
group = comp,
operators = comp.Operator,
formFamilies = comp.FormFamily.Where(ff => ff.IsActive ?? false)
).AsEnumerable().Select(i => i.group).FirstOrDefault();
To give a quick overview of what I'm trying to obtain here, I'm trying to get a list of all of the active form families associated with this company object, however, whenever I restrict the results in any way, the result set is empty.
- If the line were
formFamilies = comp.FormFamily
then it returns two results, one active one inactive - If the line is
formFamilies = comp.FormFamily.Where(ff => true)
then it returns nothing - If the line is
formFamilies = comp.FormFamily.OrderBy(ff => ff.FormFamilyId)
then it returns nothing.
Any sort of modification that I do to comp.FormFamily
means the result set returns nothing, I've dug through the deepest sections of SA to try to find a solution, and tried every solution I've found, but nothing seems to cause this list to return anything.
c# asp.net entity-framework linq-to-entities
According to this StackOverflow answer:
Linq to Entities - how to filter on child entities
you should be able to filter down the list of related entities in Entity Framework by utilizing a projection, like I've done here:
Company company = _context.Company
.Where(g => g.CompanyId == id)
.Select(comp => new
group = comp,
operators = comp.Operator,
formFamilies = comp.FormFamily.Where(ff => ff.IsActive ?? false)
).AsEnumerable().Select(i => i.group).FirstOrDefault();
To give a quick overview of what I'm trying to obtain here, I'm trying to get a list of all of the active form families associated with this company object, however, whenever I restrict the results in any way, the result set is empty.
- If the line were
formFamilies = comp.FormFamily
then it returns two results, one active one inactive - If the line is
formFamilies = comp.FormFamily.Where(ff => true)
then it returns nothing - If the line is
formFamilies = comp.FormFamily.OrderBy(ff => ff.FormFamilyId)
then it returns nothing.
Any sort of modification that I do to comp.FormFamily
means the result set returns nothing, I've dug through the deepest sections of SA to try to find a solution, and tried every solution I've found, but nothing seems to cause this list to return anything.
c# asp.net entity-framework linq-to-entities
c# asp.net entity-framework linq-to-entities
edited Nov 12 at 5:46
Foo
1
1
asked Nov 12 at 5:15
Conner Phillis
83
83
could u plz add yourCompany
andFormFamily
entity to your question?
– er-shoaib
Nov 12 at 5:36
It would be awesome if you could provide a Minimal, Complete, and Verifiable example
– JohnB
Nov 12 at 5:44
add a comment |
could u plz add yourCompany
andFormFamily
entity to your question?
– er-shoaib
Nov 12 at 5:36
It would be awesome if you could provide a Minimal, Complete, and Verifiable example
– JohnB
Nov 12 at 5:44
could u plz add your
Company
and FormFamily
entity to your question?– er-shoaib
Nov 12 at 5:36
could u plz add your
Company
and FormFamily
entity to your question?– er-shoaib
Nov 12 at 5:36
It would be awesome if you could provide a Minimal, Complete, and Verifiable example
– JohnB
Nov 12 at 5:44
It would be awesome if you could provide a Minimal, Complete, and Verifiable example
– JohnB
Nov 12 at 5:44
add a comment |
2 Answers
2
active
oldest
votes
Assuming that Company and FormFamily entities has one to many relationship I would suggest to use a join statement.Something like this should give you what you are looking for.
var company = from c in _context.Company
join f in _context.FormFamily
on c.Id equals f.CompanyId
where c.Id == id
select new Company()
Id = c.Id,
operators = c.Operator.ToList(),
formFamilies = c.FormFamily.Where(x=>x.IsActive ==
false).ToList()
.FirstOrDefault();
Hope this helps.
Thank you, I eventually solved the issue by using populating the list with a second query by using:_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();
But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax.
– Conner Phillis
Nov 12 at 18:11
Join syntax gets a bit weird in method form. Something like this might help youvar company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), );
– Farrukh Manzoor
Nov 13 at 10:11
add a comment |
I didn't quite understand what is your query is supposed to do. But it seems to me that you cannot just call Select method on another Select result method.
Anyway, you could simply use Include
methods instead of projecting.
var company = _context.Company
.Where(c => c.Id == id)
.Include(c => c.FormFamily).Where(ff => ff.IsActive ?? false)
.ToList();
Did not test it. To prove it works or not be sure put an entity model in the question. Then I may produce more accurate answer.
That won't work, when you call where on an include you will get an exception.
– Conner Phillis
Nov 12 at 18:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256285%2ffiltering-related-entites-with-entity-framework%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming that Company and FormFamily entities has one to many relationship I would suggest to use a join statement.Something like this should give you what you are looking for.
var company = from c in _context.Company
join f in _context.FormFamily
on c.Id equals f.CompanyId
where c.Id == id
select new Company()
Id = c.Id,
operators = c.Operator.ToList(),
formFamilies = c.FormFamily.Where(x=>x.IsActive ==
false).ToList()
.FirstOrDefault();
Hope this helps.
Thank you, I eventually solved the issue by using populating the list with a second query by using:_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();
But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax.
– Conner Phillis
Nov 12 at 18:11
Join syntax gets a bit weird in method form. Something like this might help youvar company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), );
– Farrukh Manzoor
Nov 13 at 10:11
add a comment |
Assuming that Company and FormFamily entities has one to many relationship I would suggest to use a join statement.Something like this should give you what you are looking for.
var company = from c in _context.Company
join f in _context.FormFamily
on c.Id equals f.CompanyId
where c.Id == id
select new Company()
Id = c.Id,
operators = c.Operator.ToList(),
formFamilies = c.FormFamily.Where(x=>x.IsActive ==
false).ToList()
.FirstOrDefault();
Hope this helps.
Thank you, I eventually solved the issue by using populating the list with a second query by using:_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();
But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax.
– Conner Phillis
Nov 12 at 18:11
Join syntax gets a bit weird in method form. Something like this might help youvar company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), );
– Farrukh Manzoor
Nov 13 at 10:11
add a comment |
Assuming that Company and FormFamily entities has one to many relationship I would suggest to use a join statement.Something like this should give you what you are looking for.
var company = from c in _context.Company
join f in _context.FormFamily
on c.Id equals f.CompanyId
where c.Id == id
select new Company()
Id = c.Id,
operators = c.Operator.ToList(),
formFamilies = c.FormFamily.Where(x=>x.IsActive ==
false).ToList()
.FirstOrDefault();
Hope this helps.
Assuming that Company and FormFamily entities has one to many relationship I would suggest to use a join statement.Something like this should give you what you are looking for.
var company = from c in _context.Company
join f in _context.FormFamily
on c.Id equals f.CompanyId
where c.Id == id
select new Company()
Id = c.Id,
operators = c.Operator.ToList(),
formFamilies = c.FormFamily.Where(x=>x.IsActive ==
false).ToList()
.FirstOrDefault();
Hope this helps.
answered Nov 12 at 6:11
Farrukh Manzoor
343
343
Thank you, I eventually solved the issue by using populating the list with a second query by using:_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();
But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax.
– Conner Phillis
Nov 12 at 18:11
Join syntax gets a bit weird in method form. Something like this might help youvar company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), );
– Farrukh Manzoor
Nov 13 at 10:11
add a comment |
Thank you, I eventually solved the issue by using populating the list with a second query by using:_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();
But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax.
– Conner Phillis
Nov 12 at 18:11
Join syntax gets a bit weird in method form. Something like this might help youvar company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), );
– Farrukh Manzoor
Nov 13 at 10:11
Thank you, I eventually solved the issue by using populating the list with a second query by using:
_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();
But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax.– Conner Phillis
Nov 12 at 18:11
Thank you, I eventually solved the issue by using populating the list with a second query by using:
_context.Entry(company) .Collection(ff => ff.FormFamily).Query().Where(ff => ff.IsActive ?? false) .Load();
But that did it in two queries. Out of curiosity, is there any way that you know to convert it to linq-to-entities? I mainly use that and would prefer not to mix syntax.– Conner Phillis
Nov 12 at 18:11
Join syntax gets a bit weird in method form. Something like this might help you
var company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), );
– Farrukh Manzoor
Nov 13 at 10:11
Join syntax gets a bit weird in method form. Something like this might help you
var company = _context.Company.FirstOrDefault(x=>x.Id == Id).Join(_dbContext.FormFamily,x => x.Id, c => c.CompanyId , (x, c) => x).Select(x => new Company() Id =x.Id,formFamilies = c.FormFamily.Where(x=>x.IsActive == false).ToList(), );
– Farrukh Manzoor
Nov 13 at 10:11
add a comment |
I didn't quite understand what is your query is supposed to do. But it seems to me that you cannot just call Select method on another Select result method.
Anyway, you could simply use Include
methods instead of projecting.
var company = _context.Company
.Where(c => c.Id == id)
.Include(c => c.FormFamily).Where(ff => ff.IsActive ?? false)
.ToList();
Did not test it. To prove it works or not be sure put an entity model in the question. Then I may produce more accurate answer.
That won't work, when you call where on an include you will get an exception.
– Conner Phillis
Nov 12 at 18:02
add a comment |
I didn't quite understand what is your query is supposed to do. But it seems to me that you cannot just call Select method on another Select result method.
Anyway, you could simply use Include
methods instead of projecting.
var company = _context.Company
.Where(c => c.Id == id)
.Include(c => c.FormFamily).Where(ff => ff.IsActive ?? false)
.ToList();
Did not test it. To prove it works or not be sure put an entity model in the question. Then I may produce more accurate answer.
That won't work, when you call where on an include you will get an exception.
– Conner Phillis
Nov 12 at 18:02
add a comment |
I didn't quite understand what is your query is supposed to do. But it seems to me that you cannot just call Select method on another Select result method.
Anyway, you could simply use Include
methods instead of projecting.
var company = _context.Company
.Where(c => c.Id == id)
.Include(c => c.FormFamily).Where(ff => ff.IsActive ?? false)
.ToList();
Did not test it. To prove it works or not be sure put an entity model in the question. Then I may produce more accurate answer.
I didn't quite understand what is your query is supposed to do. But it seems to me that you cannot just call Select method on another Select result method.
Anyway, you could simply use Include
methods instead of projecting.
var company = _context.Company
.Where(c => c.Id == id)
.Include(c => c.FormFamily).Where(ff => ff.IsActive ?? false)
.ToList();
Did not test it. To prove it works or not be sure put an entity model in the question. Then I may produce more accurate answer.
answered Nov 12 at 8:30
Serenkiy
388
388
That won't work, when you call where on an include you will get an exception.
– Conner Phillis
Nov 12 at 18:02
add a comment |
That won't work, when you call where on an include you will get an exception.
– Conner Phillis
Nov 12 at 18:02
That won't work, when you call where on an include you will get an exception.
– Conner Phillis
Nov 12 at 18:02
That won't work, when you call where on an include you will get an exception.
– Conner Phillis
Nov 12 at 18:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256285%2ffiltering-related-entites-with-entity-framework%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
could u plz add your
Company
andFormFamily
entity to your question?– er-shoaib
Nov 12 at 5:36
It would be awesome if you could provide a Minimal, Complete, and Verifiable example
– JohnB
Nov 12 at 5:44