I would suggest that you construct two data models that are able to present your data in a form that can be queried simply. As it stands, you are attempting to format the data, check for null references and query the data within a single Linq query. My choice would be to break the query down into a series of simple subqueries and check that each one is returning the expected result. I am not sure exactly what you are trying to achieve but one possible solution could be something like this.
var infoModels= _empLoyeeInfo.Where(x=>x.Party.MyAppointment.Appoint=="Y")
.Select(x=>new InfoModel(x));
var stateModels=repository.GetAll().Select(Z=>new StateModel(Z.State));
IEnumerable<EmployeeInfo> GetSelectedEmployeeInfos(IEnumerable<InfoModel> infoModels, IEnumerable<StateModel> stateModels)
{
foreach (var infoModel in infoModels)
{
foreach (var stateModel in stateModels)
{
if (infoModel.GetAreStatesMatched(stateModel))
{
yield return infoModel.EmployeeInfo;
}
}
}
}
The GetAreStatesMatched
method returns a bool. It needs to be tested to make sure that no exceptions are thrown from it.