Returned list of strings
I need to change the method mentioned below to return a list of strings. It will use contactid as input and should return a list of questionnaires
public string GetFatcaQuestionnaire(int contactId, string questionnaireType)
{
using (var context = new dbDealingContainer())
{
if (context.Connection.State == ConnectionState.Closed)
context.Connection.Open();
var fatcaQuestionaires = context.FatcaQuestionaires.FirstOrDefault(p => p.ContactID == contactId && p.QuestionnaireType == questionnaireType);
return fatcaQuestionaires != null ? fatcaQuestionaires.Questionaire : null;
}
}
New proposed method
public List<string> GetFatcaQuestionnaire(int contactId)
{
using (var context = new dbDealingContainer())
{
if (context.Connection.State == ConnectionState.Closed)
context.Connection.Open();
var fatcaQuestionaires = context.FatcaQuestionaires.Select(p => p.ContactID == contactId).ToList();
return fatcaQuestionaires.ToList();
//return fatcaQuestionaires.ToList() != null ? fatcaQuestionaires : null;
}
}
Actually only the list needs to be returned fatcaQuestonaires
. Questionaire
, not the whole object fatcaQuestonaires
. Can someone please tell me how to do this.
source to share
You almost have it. Select
calls the transform function, so it just makes a list bool
. To perform filtering you will need Where
and then Select
.
var fatcaQuestionaires = context.FatcaQuestionaires
.Where(p => p.ContactID == contactId)
.Select(p => p.Quentionaire);
return fatcaQuestionaires.ToList();
source to share
What you wrote looks like it will return a list of gates and won't compile. You need a mix of offer where
and offer select
.
return context.FatcaQuestionaires
.Where(p => p.ContactID == contactId)
.Select(p => p.Questionaire).ToList();
Where()
is what limits FactaQuesntionaires and Select()
is where you select the property to return. You can also write it like this:
return (from p in context.FatcaQuestionaires
where p.ContactID == contactId
select p.Questionaire).ToList();
source to share
Change Select
to Where
as I mentioned in my comment. Select
will just return a bool value for each input evaluation base in your lambda expression. So you getList<bool>
var fatcaQuestionaires = context.FatcaQuestionaires
.Where(p => p.ContactID == contactId)
.Select(q=>q.Questionaire).ToList();
return fatcaQuestionaires;
source to share