Take any single record if matching records not available - Linq
I have linq query which has multiple records & I am filtering these records using a where
clause.
Now, if the filtered records return nothing then I need to make it default to take default any single record from list.
var resultStaffGua = (from s in _db.Students
join sg in _db.StudentStaffGuardians on s.StudentID equals sg.StudentId
join g in _db.Staffs on sg.StaffId equals g.StaffID
join lr in _db.luRelationTypes on sg.RelationTypeId equals lr.RelationTypeID
join ga in _db.StaffAddresses on g.StaffID equals ga.StaffID
join ad in _db.Addresses on ga.AddressID equals ad.AddressID
where
lse.StatusID == (int?)Extension.StatusType.Active
&& lse.TenantID == tenantid
select new
{
g.FirstName,
g.LastName,
IsPrimary = sg.IsPrimaryGuardian,
se.Email,
Phone = sphon.PhoneNumber,
lr.RelationCD,
gdnr.GenderCD,
ad.Zipcode
}).Where(i=>i.IsPrimary==true);
if resultStaffGua
count 0
, I need one record from resultStaffGua
. Thank you
c# .net linq linq-to-entities
add a comment |
I have linq query which has multiple records & I am filtering these records using a where
clause.
Now, if the filtered records return nothing then I need to make it default to take default any single record from list.
var resultStaffGua = (from s in _db.Students
join sg in _db.StudentStaffGuardians on s.StudentID equals sg.StudentId
join g in _db.Staffs on sg.StaffId equals g.StaffID
join lr in _db.luRelationTypes on sg.RelationTypeId equals lr.RelationTypeID
join ga in _db.StaffAddresses on g.StaffID equals ga.StaffID
join ad in _db.Addresses on ga.AddressID equals ad.AddressID
where
lse.StatusID == (int?)Extension.StatusType.Active
&& lse.TenantID == tenantid
select new
{
g.FirstName,
g.LastName,
IsPrimary = sg.IsPrimaryGuardian,
se.Email,
Phone = sphon.PhoneNumber,
lr.RelationCD,
gdnr.GenderCD,
ad.Zipcode
}).Where(i=>i.IsPrimary==true);
if resultStaffGua
count 0
, I need one record from resultStaffGua
. Thank you
c# .net linq linq-to-entities
1
Whatresult
? WhatparentList
? Apart from that I don't understand the question
– MickyD
Nov 23 '18 at 7:02
@MickyD - See my update question.
– CGPA6.4
Nov 23 '18 at 7:13
@CGPA6.4, did u try below answer by JohnWu?
– er-shoaib
Nov 23 '18 at 7:13
@er-shoaib -John answer looks straight forward. That I could have thought. I am willing to do in a single line. Thank you.
– CGPA6.4
Nov 23 '18 at 7:57
add a comment |
I have linq query which has multiple records & I am filtering these records using a where
clause.
Now, if the filtered records return nothing then I need to make it default to take default any single record from list.
var resultStaffGua = (from s in _db.Students
join sg in _db.StudentStaffGuardians on s.StudentID equals sg.StudentId
join g in _db.Staffs on sg.StaffId equals g.StaffID
join lr in _db.luRelationTypes on sg.RelationTypeId equals lr.RelationTypeID
join ga in _db.StaffAddresses on g.StaffID equals ga.StaffID
join ad in _db.Addresses on ga.AddressID equals ad.AddressID
where
lse.StatusID == (int?)Extension.StatusType.Active
&& lse.TenantID == tenantid
select new
{
g.FirstName,
g.LastName,
IsPrimary = sg.IsPrimaryGuardian,
se.Email,
Phone = sphon.PhoneNumber,
lr.RelationCD,
gdnr.GenderCD,
ad.Zipcode
}).Where(i=>i.IsPrimary==true);
if resultStaffGua
count 0
, I need one record from resultStaffGua
. Thank you
c# .net linq linq-to-entities
I have linq query which has multiple records & I am filtering these records using a where
clause.
Now, if the filtered records return nothing then I need to make it default to take default any single record from list.
var resultStaffGua = (from s in _db.Students
join sg in _db.StudentStaffGuardians on s.StudentID equals sg.StudentId
join g in _db.Staffs on sg.StaffId equals g.StaffID
join lr in _db.luRelationTypes on sg.RelationTypeId equals lr.RelationTypeID
join ga in _db.StaffAddresses on g.StaffID equals ga.StaffID
join ad in _db.Addresses on ga.AddressID equals ad.AddressID
where
lse.StatusID == (int?)Extension.StatusType.Active
&& lse.TenantID == tenantid
select new
{
g.FirstName,
g.LastName,
IsPrimary = sg.IsPrimaryGuardian,
se.Email,
Phone = sphon.PhoneNumber,
lr.RelationCD,
gdnr.GenderCD,
ad.Zipcode
}).Where(i=>i.IsPrimary==true);
if resultStaffGua
count 0
, I need one record from resultStaffGua
. Thank you
c# .net linq linq-to-entities
c# .net linq linq-to-entities
edited Nov 23 '18 at 7:12
CGPA6.4
asked Nov 23 '18 at 6:58
CGPA6.4CGPA6.4
2,4821929
2,4821929
1
Whatresult
? WhatparentList
? Apart from that I don't understand the question
– MickyD
Nov 23 '18 at 7:02
@MickyD - See my update question.
– CGPA6.4
Nov 23 '18 at 7:13
@CGPA6.4, did u try below answer by JohnWu?
– er-shoaib
Nov 23 '18 at 7:13
@er-shoaib -John answer looks straight forward. That I could have thought. I am willing to do in a single line. Thank you.
– CGPA6.4
Nov 23 '18 at 7:57
add a comment |
1
Whatresult
? WhatparentList
? Apart from that I don't understand the question
– MickyD
Nov 23 '18 at 7:02
@MickyD - See my update question.
– CGPA6.4
Nov 23 '18 at 7:13
@CGPA6.4, did u try below answer by JohnWu?
– er-shoaib
Nov 23 '18 at 7:13
@er-shoaib -John answer looks straight forward. That I could have thought. I am willing to do in a single line. Thank you.
– CGPA6.4
Nov 23 '18 at 7:57
1
1
What
result
? What parentList
? Apart from that I don't understand the question– MickyD
Nov 23 '18 at 7:02
What
result
? What parentList
? Apart from that I don't understand the question– MickyD
Nov 23 '18 at 7:02
@MickyD - See my update question.
– CGPA6.4
Nov 23 '18 at 7:13
@MickyD - See my update question.
– CGPA6.4
Nov 23 '18 at 7:13
@CGPA6.4, did u try below answer by JohnWu?
– er-shoaib
Nov 23 '18 at 7:13
@CGPA6.4, did u try below answer by JohnWu?
– er-shoaib
Nov 23 '18 at 7:13
@er-shoaib -John answer looks straight forward. That I could have thought. I am willing to do in a single line. Thank you.
– CGPA6.4
Nov 23 '18 at 7:57
@er-shoaib -John answer looks straight forward. That I could have thought. I am willing to do in a single line. Thank you.
– CGPA6.4
Nov 23 '18 at 7:57
add a comment |
2 Answers
2
active
oldest
votes
if result count 0, I need one record from parentList.
Sometimes the obvious solution is the best. Why not add this after your code?
if (resultStaffGua.Count() == 0)
{
resultStaffGua = parentList.First();
}
If you want to be "clever" and do it all in one line (and I guess it would save a DB transaction too possibly) you could exchange your Where
for an OrderBy
and a Take
.
So instead of:
).Where(i=>i.IsPrimary==true);
You could do:
).OrderBy( i => i.IsPrimary ? 0 : 1 ).Take(1);
This will prioritize any record that has an IsPrimary
set to true, but it'll get one record regardless of whether any match.
Thank you for your answer. I could have think of it but I wanted solution so than I can do it in a single line. Thank you
– CGPA6.4
Nov 23 '18 at 7:42
I see. See my edit.
– John Wu
Nov 23 '18 at 8:00
add a comment |
Assuming that your intent is to retrieve one single record (there's at most one record with IsPrimary==true
):
var query = (from s in...); //The whole query except the "where"
var resultStaffGua = query.SingleOrDefault(i=>i.IsPrimary==true) ?? query.First();
Otherwise, if the query could actually return more than one result:
var query = (from s in...);
var resultStaffGua = query.Where(i=>i.IsPrimary==true);
if(resultStaffGua.Count() == 0) resultStaffGua = new { query.First(); }
Thank you for your solution. Can't I do it in a single line so that I need need to take extraquery
variable. Result directly would come inresultStaffGua
– CGPA6.4
Nov 23 '18 at 7:28
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%2f53441968%2ftake-any-single-record-if-matching-records-not-available-linq%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
if result count 0, I need one record from parentList.
Sometimes the obvious solution is the best. Why not add this after your code?
if (resultStaffGua.Count() == 0)
{
resultStaffGua = parentList.First();
}
If you want to be "clever" and do it all in one line (and I guess it would save a DB transaction too possibly) you could exchange your Where
for an OrderBy
and a Take
.
So instead of:
).Where(i=>i.IsPrimary==true);
You could do:
).OrderBy( i => i.IsPrimary ? 0 : 1 ).Take(1);
This will prioritize any record that has an IsPrimary
set to true, but it'll get one record regardless of whether any match.
Thank you for your answer. I could have think of it but I wanted solution so than I can do it in a single line. Thank you
– CGPA6.4
Nov 23 '18 at 7:42
I see. See my edit.
– John Wu
Nov 23 '18 at 8:00
add a comment |
if result count 0, I need one record from parentList.
Sometimes the obvious solution is the best. Why not add this after your code?
if (resultStaffGua.Count() == 0)
{
resultStaffGua = parentList.First();
}
If you want to be "clever" and do it all in one line (and I guess it would save a DB transaction too possibly) you could exchange your Where
for an OrderBy
and a Take
.
So instead of:
).Where(i=>i.IsPrimary==true);
You could do:
).OrderBy( i => i.IsPrimary ? 0 : 1 ).Take(1);
This will prioritize any record that has an IsPrimary
set to true, but it'll get one record regardless of whether any match.
Thank you for your answer. I could have think of it but I wanted solution so than I can do it in a single line. Thank you
– CGPA6.4
Nov 23 '18 at 7:42
I see. See my edit.
– John Wu
Nov 23 '18 at 8:00
add a comment |
if result count 0, I need one record from parentList.
Sometimes the obvious solution is the best. Why not add this after your code?
if (resultStaffGua.Count() == 0)
{
resultStaffGua = parentList.First();
}
If you want to be "clever" and do it all in one line (and I guess it would save a DB transaction too possibly) you could exchange your Where
for an OrderBy
and a Take
.
So instead of:
).Where(i=>i.IsPrimary==true);
You could do:
).OrderBy( i => i.IsPrimary ? 0 : 1 ).Take(1);
This will prioritize any record that has an IsPrimary
set to true, but it'll get one record regardless of whether any match.
if result count 0, I need one record from parentList.
Sometimes the obvious solution is the best. Why not add this after your code?
if (resultStaffGua.Count() == 0)
{
resultStaffGua = parentList.First();
}
If you want to be "clever" and do it all in one line (and I guess it would save a DB transaction too possibly) you could exchange your Where
for an OrderBy
and a Take
.
So instead of:
).Where(i=>i.IsPrimary==true);
You could do:
).OrderBy( i => i.IsPrimary ? 0 : 1 ).Take(1);
This will prioritize any record that has an IsPrimary
set to true, but it'll get one record regardless of whether any match.
edited Nov 23 '18 at 8:00
answered Nov 23 '18 at 7:06
John WuJohn Wu
29.6k42652
29.6k42652
Thank you for your answer. I could have think of it but I wanted solution so than I can do it in a single line. Thank you
– CGPA6.4
Nov 23 '18 at 7:42
I see. See my edit.
– John Wu
Nov 23 '18 at 8:00
add a comment |
Thank you for your answer. I could have think of it but I wanted solution so than I can do it in a single line. Thank you
– CGPA6.4
Nov 23 '18 at 7:42
I see. See my edit.
– John Wu
Nov 23 '18 at 8:00
Thank you for your answer. I could have think of it but I wanted solution so than I can do it in a single line. Thank you
– CGPA6.4
Nov 23 '18 at 7:42
Thank you for your answer. I could have think of it but I wanted solution so than I can do it in a single line. Thank you
– CGPA6.4
Nov 23 '18 at 7:42
I see. See my edit.
– John Wu
Nov 23 '18 at 8:00
I see. See my edit.
– John Wu
Nov 23 '18 at 8:00
add a comment |
Assuming that your intent is to retrieve one single record (there's at most one record with IsPrimary==true
):
var query = (from s in...); //The whole query except the "where"
var resultStaffGua = query.SingleOrDefault(i=>i.IsPrimary==true) ?? query.First();
Otherwise, if the query could actually return more than one result:
var query = (from s in...);
var resultStaffGua = query.Where(i=>i.IsPrimary==true);
if(resultStaffGua.Count() == 0) resultStaffGua = new { query.First(); }
Thank you for your solution. Can't I do it in a single line so that I need need to take extraquery
variable. Result directly would come inresultStaffGua
– CGPA6.4
Nov 23 '18 at 7:28
add a comment |
Assuming that your intent is to retrieve one single record (there's at most one record with IsPrimary==true
):
var query = (from s in...); //The whole query except the "where"
var resultStaffGua = query.SingleOrDefault(i=>i.IsPrimary==true) ?? query.First();
Otherwise, if the query could actually return more than one result:
var query = (from s in...);
var resultStaffGua = query.Where(i=>i.IsPrimary==true);
if(resultStaffGua.Count() == 0) resultStaffGua = new { query.First(); }
Thank you for your solution. Can't I do it in a single line so that I need need to take extraquery
variable. Result directly would come inresultStaffGua
– CGPA6.4
Nov 23 '18 at 7:28
add a comment |
Assuming that your intent is to retrieve one single record (there's at most one record with IsPrimary==true
):
var query = (from s in...); //The whole query except the "where"
var resultStaffGua = query.SingleOrDefault(i=>i.IsPrimary==true) ?? query.First();
Otherwise, if the query could actually return more than one result:
var query = (from s in...);
var resultStaffGua = query.Where(i=>i.IsPrimary==true);
if(resultStaffGua.Count() == 0) resultStaffGua = new { query.First(); }
Assuming that your intent is to retrieve one single record (there's at most one record with IsPrimary==true
):
var query = (from s in...); //The whole query except the "where"
var resultStaffGua = query.SingleOrDefault(i=>i.IsPrimary==true) ?? query.First();
Otherwise, if the query could actually return more than one result:
var query = (from s in...);
var resultStaffGua = query.Where(i=>i.IsPrimary==true);
if(resultStaffGua.Count() == 0) resultStaffGua = new { query.First(); }
answered Nov 23 '18 at 7:20
KonamimanKonamiman
42.5k1596126
42.5k1596126
Thank you for your solution. Can't I do it in a single line so that I need need to take extraquery
variable. Result directly would come inresultStaffGua
– CGPA6.4
Nov 23 '18 at 7:28
add a comment |
Thank you for your solution. Can't I do it in a single line so that I need need to take extraquery
variable. Result directly would come inresultStaffGua
– CGPA6.4
Nov 23 '18 at 7:28
Thank you for your solution. Can't I do it in a single line so that I need need to take extra
query
variable. Result directly would come in resultStaffGua
– CGPA6.4
Nov 23 '18 at 7:28
Thank you for your solution. Can't I do it in a single line so that I need need to take extra
query
variable. Result directly would come in resultStaffGua
– CGPA6.4
Nov 23 '18 at 7:28
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%2f53441968%2ftake-any-single-record-if-matching-records-not-available-linq%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
What
result
? WhatparentList
? Apart from that I don't understand the question– MickyD
Nov 23 '18 at 7:02
@MickyD - See my update question.
– CGPA6.4
Nov 23 '18 at 7:13
@CGPA6.4, did u try below answer by JohnWu?
– er-shoaib
Nov 23 '18 at 7:13
@er-shoaib -John answer looks straight forward. That I could have thought. I am willing to do in a single line. Thank you.
– CGPA6.4
Nov 23 '18 at 7:57