Checking list of material requirements availability
Models :
MaterialTemplate
int id,
MaterialType materialType,
int length,
int width,
int complexity
Material
int id,
MaterialType materialType,
int length,
int width,
int complexity,
int saleType
I have List<MaterialTemplate> requiredMaterialTemplates
;
I would like to check if each required material template is available in Material table with 2x availability, where '2x' is configurable.
I can achieve this by looping over requiredMaterialTemplates
and checking one by one with linq query but is there any way to check all the requiredMaterialTemplates
at once with linq(1 query to SQL).
Also, how can we get number of materials required for each material template if provided availability is 2x without looping every template in requiredMaterialTemplates
.
Example:
Required materials with Availability value as 2x
MaterialTemplateId:1,MaterialType:"Bronze",Complexity:"Solid"
MaterialTemplateId:2,MaterialType:"Silver",Complexity:"Solid",Length:10
Materials in db
1,"Bronze",10,5,"Solid","Limited"
2,"Bronze",20,6,"Solid","Limited"
3,"Silver",10,5,"Solid","Limited"
4,"Copper",10,5,"Solid","Limited"
Result should be
MaterialTemplateId, Required Count
1,0
2,1
c# linq
add a comment |
Models :
MaterialTemplate
int id,
MaterialType materialType,
int length,
int width,
int complexity
Material
int id,
MaterialType materialType,
int length,
int width,
int complexity,
int saleType
I have List<MaterialTemplate> requiredMaterialTemplates
;
I would like to check if each required material template is available in Material table with 2x availability, where '2x' is configurable.
I can achieve this by looping over requiredMaterialTemplates
and checking one by one with linq query but is there any way to check all the requiredMaterialTemplates
at once with linq(1 query to SQL).
Also, how can we get number of materials required for each material template if provided availability is 2x without looping every template in requiredMaterialTemplates
.
Example:
Required materials with Availability value as 2x
MaterialTemplateId:1,MaterialType:"Bronze",Complexity:"Solid"
MaterialTemplateId:2,MaterialType:"Silver",Complexity:"Solid",Length:10
Materials in db
1,"Bronze",10,5,"Solid","Limited"
2,"Bronze",20,6,"Solid","Limited"
3,"Silver",10,5,"Solid","Limited"
4,"Copper",10,5,"Solid","Limited"
Result should be
MaterialTemplateId, Required Count
1,0
2,1
c# linq
1
Does MaterialTemplate have an id of the Material item or something? How you can get the Material asociated to it?
– AlbertoCh
Nov 14 '18 at 12:43
Could you provide some sample data and expect result?
– D-Shih
Nov 14 '18 at 12:43
@AlbertoCh yes, it has Id as in the question edited.
– suraj13k
Nov 14 '18 at 13:43
@D-Shih Updated the question with sample data and expected result
– suraj13k
Nov 15 '18 at 9:27
add a comment |
Models :
MaterialTemplate
int id,
MaterialType materialType,
int length,
int width,
int complexity
Material
int id,
MaterialType materialType,
int length,
int width,
int complexity,
int saleType
I have List<MaterialTemplate> requiredMaterialTemplates
;
I would like to check if each required material template is available in Material table with 2x availability, where '2x' is configurable.
I can achieve this by looping over requiredMaterialTemplates
and checking one by one with linq query but is there any way to check all the requiredMaterialTemplates
at once with linq(1 query to SQL).
Also, how can we get number of materials required for each material template if provided availability is 2x without looping every template in requiredMaterialTemplates
.
Example:
Required materials with Availability value as 2x
MaterialTemplateId:1,MaterialType:"Bronze",Complexity:"Solid"
MaterialTemplateId:2,MaterialType:"Silver",Complexity:"Solid",Length:10
Materials in db
1,"Bronze",10,5,"Solid","Limited"
2,"Bronze",20,6,"Solid","Limited"
3,"Silver",10,5,"Solid","Limited"
4,"Copper",10,5,"Solid","Limited"
Result should be
MaterialTemplateId, Required Count
1,0
2,1
c# linq
Models :
MaterialTemplate
int id,
MaterialType materialType,
int length,
int width,
int complexity
Material
int id,
MaterialType materialType,
int length,
int width,
int complexity,
int saleType
I have List<MaterialTemplate> requiredMaterialTemplates
;
I would like to check if each required material template is available in Material table with 2x availability, where '2x' is configurable.
I can achieve this by looping over requiredMaterialTemplates
and checking one by one with linq query but is there any way to check all the requiredMaterialTemplates
at once with linq(1 query to SQL).
Also, how can we get number of materials required for each material template if provided availability is 2x without looping every template in requiredMaterialTemplates
.
Example:
Required materials with Availability value as 2x
MaterialTemplateId:1,MaterialType:"Bronze",Complexity:"Solid"
MaterialTemplateId:2,MaterialType:"Silver",Complexity:"Solid",Length:10
Materials in db
1,"Bronze",10,5,"Solid","Limited"
2,"Bronze",20,6,"Solid","Limited"
3,"Silver",10,5,"Solid","Limited"
4,"Copper",10,5,"Solid","Limited"
Result should be
MaterialTemplateId, Required Count
1,0
2,1
c# linq
c# linq
edited Nov 15 '18 at 9:26
suraj13k
asked Nov 14 '18 at 12:40
suraj13ksuraj13k
1008
1008
1
Does MaterialTemplate have an id of the Material item or something? How you can get the Material asociated to it?
– AlbertoCh
Nov 14 '18 at 12:43
Could you provide some sample data and expect result?
– D-Shih
Nov 14 '18 at 12:43
@AlbertoCh yes, it has Id as in the question edited.
– suraj13k
Nov 14 '18 at 13:43
@D-Shih Updated the question with sample data and expected result
– suraj13k
Nov 15 '18 at 9:27
add a comment |
1
Does MaterialTemplate have an id of the Material item or something? How you can get the Material asociated to it?
– AlbertoCh
Nov 14 '18 at 12:43
Could you provide some sample data and expect result?
– D-Shih
Nov 14 '18 at 12:43
@AlbertoCh yes, it has Id as in the question edited.
– suraj13k
Nov 14 '18 at 13:43
@D-Shih Updated the question with sample data and expected result
– suraj13k
Nov 15 '18 at 9:27
1
1
Does MaterialTemplate have an id of the Material item or something? How you can get the Material asociated to it?
– AlbertoCh
Nov 14 '18 at 12:43
Does MaterialTemplate have an id of the Material item or something? How you can get the Material asociated to it?
– AlbertoCh
Nov 14 '18 at 12:43
Could you provide some sample data and expect result?
– D-Shih
Nov 14 '18 at 12:43
Could you provide some sample data and expect result?
– D-Shih
Nov 14 '18 at 12:43
@AlbertoCh yes, it has Id as in the question edited.
– suraj13k
Nov 14 '18 at 13:43
@AlbertoCh yes, it has Id as in the question edited.
– suraj13k
Nov 14 '18 at 13:43
@D-Shih Updated the question with sample data and expected result
– suraj13k
Nov 15 '18 at 9:27
@D-Shih Updated the question with sample data and expected result
– suraj13k
Nov 15 '18 at 9:27
add a comment |
2 Answers
2
active
oldest
votes
I think you can use the group by
to get all the materialType you have twice or more.
Something like this:
from m in Material
group m by m.MaterialType into grp
where grp.Count() > 1
select grp.Key
add a comment |
If I understood right what you need:
from material in availableMaterials
join template in requiredMaterialTemplates
on new material.materialType, material.length, material.width, material.complexity
equals new template.materialType, template.length, template.width, template.complexity
where material.quantity < 2
select material
Here I assume you have List<Material> availableMaterials
.
I think this won't work unless we do a group by.
– suraj13k
Nov 14 '18 at 13:46
@suraj13k, it depends from how to interpret records inMaterial
table. I assume there is one record per material andquantity
means how much this material we have. In this case my query will works.
– Miamy
Nov 14 '18 at 14:12
Okay, Please check updated question with sample data and expected result. Sorry for the confusion.
– suraj13k
Nov 15 '18 at 9:28
Excuse me, but now I don't understand which criteria should be used for desired results. Why bronze's required count is 0 if we have 2 bronze's records in Materials table? In other words, what is criteria for the availability? Is it number of records, product of length and width or something more complicate?
– Miamy
Nov 15 '18 at 11:13
criteria can be dynamic, one template might be having only length of 10 and other template might have length, width, etc. Yes this is complicated :(
– suraj13k
Nov 15 '18 at 12:24
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%2f53300478%2fchecking-list-of-material-requirements-availability%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
I think you can use the group by
to get all the materialType you have twice or more.
Something like this:
from m in Material
group m by m.MaterialType into grp
where grp.Count() > 1
select grp.Key
add a comment |
I think you can use the group by
to get all the materialType you have twice or more.
Something like this:
from m in Material
group m by m.MaterialType into grp
where grp.Count() > 1
select grp.Key
add a comment |
I think you can use the group by
to get all the materialType you have twice or more.
Something like this:
from m in Material
group m by m.MaterialType into grp
where grp.Count() > 1
select grp.Key
I think you can use the group by
to get all the materialType you have twice or more.
Something like this:
from m in Material
group m by m.MaterialType into grp
where grp.Count() > 1
select grp.Key
answered Nov 14 '18 at 12:46
PapluscPaplusc
3231414
3231414
add a comment |
add a comment |
If I understood right what you need:
from material in availableMaterials
join template in requiredMaterialTemplates
on new material.materialType, material.length, material.width, material.complexity
equals new template.materialType, template.length, template.width, template.complexity
where material.quantity < 2
select material
Here I assume you have List<Material> availableMaterials
.
I think this won't work unless we do a group by.
– suraj13k
Nov 14 '18 at 13:46
@suraj13k, it depends from how to interpret records inMaterial
table. I assume there is one record per material andquantity
means how much this material we have. In this case my query will works.
– Miamy
Nov 14 '18 at 14:12
Okay, Please check updated question with sample data and expected result. Sorry for the confusion.
– suraj13k
Nov 15 '18 at 9:28
Excuse me, but now I don't understand which criteria should be used for desired results. Why bronze's required count is 0 if we have 2 bronze's records in Materials table? In other words, what is criteria for the availability? Is it number of records, product of length and width or something more complicate?
– Miamy
Nov 15 '18 at 11:13
criteria can be dynamic, one template might be having only length of 10 and other template might have length, width, etc. Yes this is complicated :(
– suraj13k
Nov 15 '18 at 12:24
add a comment |
If I understood right what you need:
from material in availableMaterials
join template in requiredMaterialTemplates
on new material.materialType, material.length, material.width, material.complexity
equals new template.materialType, template.length, template.width, template.complexity
where material.quantity < 2
select material
Here I assume you have List<Material> availableMaterials
.
I think this won't work unless we do a group by.
– suraj13k
Nov 14 '18 at 13:46
@suraj13k, it depends from how to interpret records inMaterial
table. I assume there is one record per material andquantity
means how much this material we have. In this case my query will works.
– Miamy
Nov 14 '18 at 14:12
Okay, Please check updated question with sample data and expected result. Sorry for the confusion.
– suraj13k
Nov 15 '18 at 9:28
Excuse me, but now I don't understand which criteria should be used for desired results. Why bronze's required count is 0 if we have 2 bronze's records in Materials table? In other words, what is criteria for the availability? Is it number of records, product of length and width or something more complicate?
– Miamy
Nov 15 '18 at 11:13
criteria can be dynamic, one template might be having only length of 10 and other template might have length, width, etc. Yes this is complicated :(
– suraj13k
Nov 15 '18 at 12:24
add a comment |
If I understood right what you need:
from material in availableMaterials
join template in requiredMaterialTemplates
on new material.materialType, material.length, material.width, material.complexity
equals new template.materialType, template.length, template.width, template.complexity
where material.quantity < 2
select material
Here I assume you have List<Material> availableMaterials
.
If I understood right what you need:
from material in availableMaterials
join template in requiredMaterialTemplates
on new material.materialType, material.length, material.width, material.complexity
equals new template.materialType, template.length, template.width, template.complexity
where material.quantity < 2
select material
Here I assume you have List<Material> availableMaterials
.
edited Nov 14 '18 at 14:10
answered Nov 14 '18 at 13:21
MiamyMiamy
1,083715
1,083715
I think this won't work unless we do a group by.
– suraj13k
Nov 14 '18 at 13:46
@suraj13k, it depends from how to interpret records inMaterial
table. I assume there is one record per material andquantity
means how much this material we have. In this case my query will works.
– Miamy
Nov 14 '18 at 14:12
Okay, Please check updated question with sample data and expected result. Sorry for the confusion.
– suraj13k
Nov 15 '18 at 9:28
Excuse me, but now I don't understand which criteria should be used for desired results. Why bronze's required count is 0 if we have 2 bronze's records in Materials table? In other words, what is criteria for the availability? Is it number of records, product of length and width or something more complicate?
– Miamy
Nov 15 '18 at 11:13
criteria can be dynamic, one template might be having only length of 10 and other template might have length, width, etc. Yes this is complicated :(
– suraj13k
Nov 15 '18 at 12:24
add a comment |
I think this won't work unless we do a group by.
– suraj13k
Nov 14 '18 at 13:46
@suraj13k, it depends from how to interpret records inMaterial
table. I assume there is one record per material andquantity
means how much this material we have. In this case my query will works.
– Miamy
Nov 14 '18 at 14:12
Okay, Please check updated question with sample data and expected result. Sorry for the confusion.
– suraj13k
Nov 15 '18 at 9:28
Excuse me, but now I don't understand which criteria should be used for desired results. Why bronze's required count is 0 if we have 2 bronze's records in Materials table? In other words, what is criteria for the availability? Is it number of records, product of length and width or something more complicate?
– Miamy
Nov 15 '18 at 11:13
criteria can be dynamic, one template might be having only length of 10 and other template might have length, width, etc. Yes this is complicated :(
– suraj13k
Nov 15 '18 at 12:24
I think this won't work unless we do a group by.
– suraj13k
Nov 14 '18 at 13:46
I think this won't work unless we do a group by.
– suraj13k
Nov 14 '18 at 13:46
@suraj13k, it depends from how to interpret records in
Material
table. I assume there is one record per material and quantity
means how much this material we have. In this case my query will works.– Miamy
Nov 14 '18 at 14:12
@suraj13k, it depends from how to interpret records in
Material
table. I assume there is one record per material and quantity
means how much this material we have. In this case my query will works.– Miamy
Nov 14 '18 at 14:12
Okay, Please check updated question with sample data and expected result. Sorry for the confusion.
– suraj13k
Nov 15 '18 at 9:28
Okay, Please check updated question with sample data and expected result. Sorry for the confusion.
– suraj13k
Nov 15 '18 at 9:28
Excuse me, but now I don't understand which criteria should be used for desired results. Why bronze's required count is 0 if we have 2 bronze's records in Materials table? In other words, what is criteria for the availability? Is it number of records, product of length and width or something more complicate?
– Miamy
Nov 15 '18 at 11:13
Excuse me, but now I don't understand which criteria should be used for desired results. Why bronze's required count is 0 if we have 2 bronze's records in Materials table? In other words, what is criteria for the availability? Is it number of records, product of length and width or something more complicate?
– Miamy
Nov 15 '18 at 11:13
criteria can be dynamic, one template might be having only length of 10 and other template might have length, width, etc. Yes this is complicated :(
– suraj13k
Nov 15 '18 at 12:24
criteria can be dynamic, one template might be having only length of 10 and other template might have length, width, etc. Yes this is complicated :(
– suraj13k
Nov 15 '18 at 12:24
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.
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%2f53300478%2fchecking-list-of-material-requirements-availability%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
1
Does MaterialTemplate have an id of the Material item or something? How you can get the Material asociated to it?
– AlbertoCh
Nov 14 '18 at 12:43
Could you provide some sample data and expect result?
– D-Shih
Nov 14 '18 at 12:43
@AlbertoCh yes, it has Id as in the question edited.
– suraj13k
Nov 14 '18 at 13:43
@D-Shih Updated the question with sample data and expected result
– suraj13k
Nov 15 '18 at 9:27