Select new with an if statement inside
I have this piece of code that select new some info from the database in my C# project but I only want it to do it if the name is not a certain name so here is the code
public List<Info> GetInfo(int id)
{
var cacheKey = "allinfo-" + id;
var info = SoftCache.GetData<List<info>>(cacheKey);
if (info != null)
return info;
using (var db = DB.InfoModel)
{
info = (from j in db.info_list()
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
SoftCache.Add(cacheKey, info, new TimeSpan(0, 0, 5), new TimeSpan(0, 1, 0));
return info;
}
What I want is something like
if(j.info_name != "BadName"){
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
c# .net linq if-statement
add a comment |
I have this piece of code that select new some info from the database in my C# project but I only want it to do it if the name is not a certain name so here is the code
public List<Info> GetInfo(int id)
{
var cacheKey = "allinfo-" + id;
var info = SoftCache.GetData<List<info>>(cacheKey);
if (info != null)
return info;
using (var db = DB.InfoModel)
{
info = (from j in db.info_list()
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
SoftCache.Add(cacheKey, info, new TimeSpan(0, 0, 5), new TimeSpan(0, 1, 0));
return info;
}
What I want is something like
if(j.info_name != "BadName"){
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
c# .net linq if-statement
I have no idea what you want. You want to filter byinfo_name
, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what yourif
suggests)?
– Rango
Nov 28 '18 at 9:39
add a comment |
I have this piece of code that select new some info from the database in my C# project but I only want it to do it if the name is not a certain name so here is the code
public List<Info> GetInfo(int id)
{
var cacheKey = "allinfo-" + id;
var info = SoftCache.GetData<List<info>>(cacheKey);
if (info != null)
return info;
using (var db = DB.InfoModel)
{
info = (from j in db.info_list()
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
SoftCache.Add(cacheKey, info, new TimeSpan(0, 0, 5), new TimeSpan(0, 1, 0));
return info;
}
What I want is something like
if(j.info_name != "BadName"){
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
c# .net linq if-statement
I have this piece of code that select new some info from the database in my C# project but I only want it to do it if the name is not a certain name so here is the code
public List<Info> GetInfo(int id)
{
var cacheKey = "allinfo-" + id;
var info = SoftCache.GetData<List<info>>(cacheKey);
if (info != null)
return info;
using (var db = DB.InfoModel)
{
info = (from j in db.info_list()
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
SoftCache.Add(cacheKey, info, new TimeSpan(0, 0, 5), new TimeSpan(0, 1, 0));
return info;
}
What I want is something like
if(j.info_name != "BadName"){
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
}
c# .net linq if-statement
c# .net linq if-statement
edited Nov 28 '18 at 10:32
S.Akbari
30.5k93774
30.5k93774
asked Nov 28 '18 at 9:36
Johan JönssonJohan Jönsson
1048
1048
I have no idea what you want. You want to filter byinfo_name
, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what yourif
suggests)?
– Rango
Nov 28 '18 at 9:39
add a comment |
I have no idea what you want. You want to filter byinfo_name
, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what yourif
suggests)?
– Rango
Nov 28 '18 at 9:39
I have no idea what you want. You want to filter by
info_name
, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if
suggests)?– Rango
Nov 28 '18 at 9:39
I have no idea what you want. You want to filter by
info_name
, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if
suggests)?– Rango
Nov 28 '18 at 9:39
add a comment |
3 Answers
3
active
oldest
votes
You need to add a where statement here:
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
add a comment |
Or by using Where
extension method
var goodNames =
db.info_list().Where(info => info.info_name != "Badname")
.Select(info => new Info
{
InfoName = info.info_name,
InfoId = info.info_id,
InfoValue = info.info_value,
})
.ToList();
add a comment |
You may use a where to filter the info as follow:-
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
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%2f53516309%2fselect-new-with-an-if-statement-inside%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to add a where statement here:
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
add a comment |
You need to add a where statement here:
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
add a comment |
You need to add a where statement here:
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
You need to add a where statement here:
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
edited Nov 28 '18 at 9:44
answered Nov 28 '18 at 9:38
S.AkbariS.Akbari
30.5k93774
30.5k93774
add a comment |
add a comment |
Or by using Where
extension method
var goodNames =
db.info_list().Where(info => info.info_name != "Badname")
.Select(info => new Info
{
InfoName = info.info_name,
InfoId = info.info_id,
InfoValue = info.info_value,
})
.ToList();
add a comment |
Or by using Where
extension method
var goodNames =
db.info_list().Where(info => info.info_name != "Badname")
.Select(info => new Info
{
InfoName = info.info_name,
InfoId = info.info_id,
InfoValue = info.info_value,
})
.ToList();
add a comment |
Or by using Where
extension method
var goodNames =
db.info_list().Where(info => info.info_name != "Badname")
.Select(info => new Info
{
InfoName = info.info_name,
InfoId = info.info_id,
InfoValue = info.info_value,
})
.ToList();
Or by using Where
extension method
var goodNames =
db.info_list().Where(info => info.info_name != "Badname")
.Select(info => new Info
{
InfoName = info.info_name,
InfoId = info.info_id,
InfoValue = info.info_value,
})
.ToList();
answered Nov 28 '18 at 9:40
FabioFabio
20.1k22048
20.1k22048
add a comment |
add a comment |
You may use a where to filter the info as follow:-
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
add a comment |
You may use a where to filter the info as follow:-
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
add a comment |
You may use a where to filter the info as follow:-
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
You may use a where to filter the info as follow:-
info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();
edited Nov 28 '18 at 9:46
answered Nov 28 '18 at 9:39
Afnan AhmadAfnan Ahmad
1,89121533
1,89121533
add a comment |
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%2f53516309%2fselect-new-with-an-if-statement-inside%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
I have no idea what you want. You want to filter by
info_name
, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what yourif
suggests)?– Rango
Nov 28 '18 at 9:39