Tom
Tom

Reputation: 8681

Return list of string

I need to modify the method mentioned below to return list of strings . It would take contactid as input and should return 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 need to return a list of only fatcaQuestonaires. Questionaire and not the whole fatcaQuestonaires object. Could some one tell me how to go about it.

Upvotes: 3

Views: 591

Answers (5)

Marc Cals
Marc Cals

Reputation: 2989

Using Linq, first you can do a Where filtering the desired rows, and then Select to projecting only Questionaire properties.

Try this

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

Upvotes: 6

Shashank Shekhar
Shashank Shekhar

Reputation: 4178

Change Select to Where as I mentioned in my comment. Select will just return a bool value for every entry's evaluation base on your lambda expression. So you end up with a List<bool>

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

Upvotes: 1

Berin Loritsch
Berin Loritsch

Reputation: 11463

What you have written looks like it will return a list of booleans and not compile. What you need is a combination of a where clause and a select clause.

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

Where() is what limits the FactaQuesntionaires, and Select() is where you choose 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();

Upvotes: 2

David Forshner
David Forshner

Reputation: 163

Project out the property you want with .Select(x => x.MyProp);

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

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61349

You almost have it. Select invokes a transformation function, so it is just making a list of bool. You need a Where clause to do the filtering, and then a Select.

var fatcaQuestionaires = context.FatcaQuestionaires
                        .Where(p => p.ContactID == contactId)
                        .Select(p => p.Quentionaire);

return fatcaQuestionaires.ToList();

Upvotes: 4

Related Questions