0
List<PrpSubjects> objListSubjects  = _objSubjectDal.GetAllSubjects();
ddlSubjects.DataSource = objListSubjects;
ddlSubjects.DataBind();

_subjectName = objListSubjects...?

In _subjectName I want to fetch the subjectname from objListSubjects on basis of subjectid. The subject list has subjectid and subjectname columns.

the question is i have a list with 2 columns subjectid,subjectname... the method returns a list of subject now i want to fetch the subjectname by subjectid,,, i thght instead of querying the database again i thght to use linq on list to fetch the subject name.. i hope i am clear about my requirement

1
  • dropdown... the question is i have a list<Subject> with 2 columns subjectid,subjectname... the method returns a list of subject now i want to fetch the subjectname by subjectid,,, i thght instead of querying the database again i thght to use linq on list to fetch the subject name.. i hope i am clear about my requirement Commented May 23, 2011 at 9:22

3 Answers 3

2
_subjectName = objListSubjects
               .Where(s => s.SubjectId == someId)
               .Select(s => s.SubjectName)
               .FirstOrDefault();

(will return null if there is no subject with the id someId)

Sign up to request clarification or add additional context in comments.

Comments

0
_subjectName = objListSubjects.First(s => s.SubjectID == theIdYouAlreadyHave).SubjectName;

If you suspect that subject might not exist, you can use

objListSubjects.FirstOrDefault(s => s.SubjectID == id);

That will return null if it doesn't exist.

Comments

0

Or, if you find the sql style better to read ;)

_subjectName = (from s in objListSubjects 
               where s.SubjectId == someId 
               select s.SubjectName).FirstOrDefault();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.