Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi team,
I have different objects with the list of items could you please suggest how to check conditions with the existing lambda expression code itself

Below is I am getting an exception that index was out of bound exception due to using array position to get value to compare with other value.

var empLoyeeInfo= _empLoyeeInfo.SelectMany(x=>x.Party.MyAppointment.Where(y=>y.Appoint="Y"
            && x.AppointName[1] == repository.GetAll().FirstorDefault(Z=>Z.State == y.StateCode).StateCode.Split(',')[0]
            || x.AppointName[1] == repository.GetAll().FirstorDefault(Z => Z.State == y.StateCode).StateCode.Split(',')[1]
            ))


What I have tried:

var empLoyeeInfo= _empLoyeeInfo.SelectMany(x=>x.Party.MyAppointment.Where(y=>y.Appoint="Y"
            && x.AppointName[1] == repository.GetAll().FirstorDefault(Z=>Z.State == y.StateCode).StateCode.Substring(0,1)
            || x.AppointName[1] == repository.GetAll().FirstorDefault(Z => Z.State == y.StateCode).StateCode.Substring(1,1)
            ))


Note I want to get a better approach without using index type of getting values
Please assist out to achieve without using [0] and [1]

Comparing values like below

State = "A"
StaeCode = "PA"

Comparison like A= 2nd char of StaeCOde A ie: A = A
Posted
Updated 8-Aug-22 5:09am
Comments
[no name] 4-Aug-22 14:01pm    
Probably simpler if you use "StartsWith" or "EndsWith". But I question the whole approach and point of checking characters in a 2 character state code that has zero significance other than to point to a state name.
George Swan 5-Aug-22 1:35am    
It seems to me that the first line of your 'what you have tried' code has an error, you need to change '=' to '=='. It should read, Where(y=>y.Appoint=="Y"

If you are getting out of range exceptions with indexes of zero and one, the simplest reason is that the collection (List, array, etc.) contains no elements, or just one. There isn't anything we can do to fix that - you need to look at where you get the data from and start thinking about what data you are actually processing.

So start by using the debugger to see exactly what the data is, and how much of it there is - then start looking back through your code to find out why.

Sorry, but we can't do any of that for you!
 
Share this answer
 

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.

C#
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.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900