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.

+3


source to share


5 answers


Extract the required property. Select (x => x.MyProp);



return fatcaQuestionaires.Select(x => x.Questionaire).ToList();

      

0


source


Using Linq, you can first do the Where

filtering on the rows you want and then Select

to project only the Questionaire properties.

try it



return context.FatcaQuestionaires
    .Where(p => p.ContactID == contactId)
    .Select(p => p.Questionaire)
    .ToList();

      

+6


source


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();

      

+4


source


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();

      

+2


source


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;

      

+1


source







All Articles