Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

Filter by Commit ID

I have a search page that filter data and display the search result.
one of the dropdown list is CommitID




                   list = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().ToList();

                    //Filter CommitID
                    if (model.SelectedCommitID != null)
                        list = list.Where(x => model.SelectedCommitID .Any(m => Equals(x.CommitID, m)));



the data in the database looks like this:

Table: RuleDetailRepo
RuleDetailID                       CommitID 
1                                         4534565434, 488573377
2                                         7876545665
3                                         9878787678, 39484723, 494858573
4                                         8475637364
5                                         8374478534, 95574746 

but the dropdown list 
list the data like this: 

                          <select name="SelectedCommitID " class="btn btn-default dropdown-toggle" id="SelectedCommitID " multiple="multiple">

                                        <option value="4534565434">4534565434</option>

                                        <option value="488573377">488573377</option>

                                        <option value="7876545665">7876545665</option>

                                        <option value="9878787678">9878787678</option>

                                        <option value="39484723">39484723</option>

                                        <option value="494858573">494858573</option>

                                        <option value="8475637364">8475637364</option>

                                        <option value="8374478534">8374478534</option>

                                        <option value="95574746 ">95574746 </option>

                            </select>


when the user multi select 7876545665 and 8374478534 and 95574746 
the system should return three records. 

Open in new window


Thanks,
Lulu50
Avatar of lulu50
lulu50
Flag of United States of America image

ASKER

something like this but I'm not getting any return data.

so that means my code is wrong.

 if (model.SelectedCommitID != null)
                    {
                        foreach (var GetCommitID in model.SelectedCommitID)
                        {
                            list = list.Where(x => GetCommitID.Any(m => Equals(x.CommitID, m)));
                        }
                    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of srikanthreddyn143
srikanthreddyn143

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lulu50

ASKER

srikanthreddyn143,

Thank you for helping me.

I populate my dropdown list like this:

 public IEnumerable<SelectListItem> GetAllCommitIDList()
        {
            return string.Join(", ", _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().Where(x => !string.IsNullOrWhiteSpace(x.CommitID)).Select(x => x.CommitID))
                .Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Trim())
                .Distinct()
                .Select(x => new SelectListItem { Text = x, Value = x }).OrderBy(x => x.Text);
        }

Open in new window



I changed my code but I'm not getting any data

 //Filter CommitID
                    if (model.SelectedCommitID != null)
                    {
                            list = list.Where(x => model.SelectedCommitID.Any(m => Array.IndexOf(x.CommitID.Split(','), m) > -1));
                    }

Open in new window



Table: RuleDetailRepo
RuleDetailID                       CommitID
1                                         4534565434, 488573377
2                                         7876545665
3                                         9878787678, 39484723, 494858573
4                                         8475637364
5                                         8374478534, 95574746

if the user select 488573377  and 8374478534
I want to return record #1 and #5
Avatar of lulu50

ASKER

I'm getting this error

An exception of type 'System.NullReferenceException' occurred in BCUW.Service.dll but was not handled in user code


 list = list.Where(x => model.SelectedCommitID.Any(m => Equals(Array.IndexOf(x.CommitID.Split(','), m))));
Avatar of srikanthreddyn143
srikanthreddyn143

You should use array. Index >-1 check instead of equals.
Avatar of lulu50

ASKER

I had 18 records before it execute the code below.
right after this code, I'm getting no data.

if (model.SelectedCommitID != null)
                    {
                              list = list.Where(x => model.SelectedCommitID.Any(m => Array.IndexOf(x.CommitID.Split(','), m) > -1));
               
                    }

I'm getting this error message:


Object reference not set to an instance of an object.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

   list = list.Where(x => model.SelectedCommitID.Any(m => Array.IndexOf(x.CommitID.Split(','), m) > -1));


I'm not sure how to fix this.
Do you mind posting your full code and where the error is happening?
Avatar of lulu50

ASKER

Filter the CommitID is where I'm getting the error 


   public List<RuleDetail> RuleDetailList(CABRSearchRules model)
        {
            List<RuleDetail> modelList = new List<RuleDetail>();

            IEnumerable<CABR_RuleDetail> list = new List<CABR_RuleDetail>();
            IEnumerable<CABR_HistoryRuleDetail> Historylist = new List<CABR_HistoryRuleDetail>();

            if (model.flagManagementList)
            {
                var GetRuleset = _unitOfWorkCABusinessRules.RuleSetRepo.GetAll()
             .Where(x => x.IsActive == true).ToList();

                var GetRuleIdentifier = _unitOfWorkCABusinessRules.RuleIdentifierRepo.GetAll()
                    .Where(x => x.IsActive == true).ToList();


                if (model.SelectedCurrentOrHistoryID == 1 || model.SelectedCurrentOrHistoryID == null) //Current
                {

                    list = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().ToList();

                    //Filter Status
                    if (model.SelectedStatus != null)
                        list = list.Where(x => x.isActive == model.SelectedStatus);

                    //Filter Environment
                    if (model.SelectedEnvironment != "" & model.SelectedEnvironment != null)
                        list = list.Where(x => x.Environment == model.SelectedEnvironment);

                    //Filter Version
                    if (model.SelectedVersion != null)
                        list = list.Where(x => model.SelectedVersion.Any(m => Equals(x.Version, m)));

                    //Filter CQ Ticket
                    if (model.SelectedCQticket != null)
                        list = list.Where(x => model.SelectedCQticket.Any(m => Equals(x.CQTicket, m)));

                    //Filter CommitID
                    if (model.SelectedCommitID != null)
                    {
                        list = list.Where(x => model.SelectedCommitID.Any(m => Array.IndexOf(x.CommitID.Split(','), m) > -1));
                    }

                    //Filter Created By
                    if (model.SelectedCreatedBy != null)
                        list = list.Where(x => model.SelectedCreatedBy.Any(m => Equals(x.CreatedBy, m)));

                    //Filter Rule Set
                    if (model.SelectedRuleSet != null)
                        list = list.Where(x => model.SelectedRuleSet.Any(m => Equals(x.RuleSetId, m)));

                    //Filter Rule Identifier 
                    if (model.SelectedRuleIdentifier != null)
                        list = list.Where(x => model.SelectedRuleIdentifier.Any(m => Equals(x.RuleIdentifierId, m)));

                    //Filter Created Date
                    if (model.FromCreatedDate.ToString() != "1/1/0001 12:00:00 AM" && model.ToCreatedDate.ToString() != "1/1/0001 12:00:00 AM")
                        list = list.Where(x => x.CreatedDate.Date >= model.FromCreatedDate.Date && x.CreatedDate.Date <= model.ToCreatedDate.Date);

                    //Filter Migration Date
                    if (model.FromMigrationDate.ToString() != "1/1/0001 12:00:00 AM" && model.ToMigrationDate.ToString() != "1/1/0001 12:00:00 AM")
                        list = list.Where(x => x.MigrationDate.Date >= model.FromMigrationDate.Date && x.MigrationDate.Date <= model.ToMigrationDate.Date);

                    //Filter Migration Date
                    if (model.RuleDescription != null)
                        list = list.Where(x => x.RuleDescription.Contains(model.RuleDescription));

                    //Filter Condition Attributes And Values
                    if (model.ConditionAttributesAndValues != null)
                        list = list.Where(x => x.ConditionAttributesAndValues.Contains(model.ConditionAttributesAndValues));

                    //Filter Action Attributes And Values
                    if (model.ActionAttributesAndValues != null)
                        list = list.Where(x => x.ActionAttributesAndValues.Contains(model.ActionAttributesAndValues));

                    //Filter Comment Or Notes
                    if (model.CommentOrNotes != null)
                        list = list.Where(x => x.CommentOrNotes.Contains(model.CommentOrNotes));

                    //Filter Commit Description
                    if (model.CommitDescription != null && model.SelectedFilterList.ToArray().Any(model.CommitDescription.Equals))
                        list = list.Where(x => x.CommitDescription.Contains(model.CommitDescription));

                    //Filter Selected Function
                    if (model.SelectedFunction != null)
                        list = list.Where(x => model.SelectedFunction.Any(x.ConditionAttributesAndValues.Contains));

                    //Filter Selected Group
                    if (model.SelectedGroup != null && model.SelectedGroup[0] != "")
                        list = list.Where(x => model.SelectedGroup.Any(x.ConditionAttributesAndValues.Contains));

                    //Filter Group Txt
                    if (model.GroupTxt != null)
                        list = list.Where(x => InListOrRange(model.GroupTxt, x.ConditionAttributesAndValues));

                    modelList = list.ToList().Select(x => new RuleDetail
                    {
                        Status = (bool)x.isActive,
                        RuleSet = GetRuleset.Where(a => a.RuleSetId == x.RuleSetId).FirstOrDefault().RuleSet,
                        RuleIdentifier = GetRuleIdentifier.Where(a => a.RuleIdentifierId == x.RuleIdentifierId).FirstOrDefault().RuleIdentifier,
                        Versions = x.Version,
                        Environments = x.Environment,
                        CommitID = x.CommitID,
                        CommitDescription = x.CommitDescription,
                        RuleDescription = x.RuleDescription,
                        ConditionAttributesAndValues = x.ConditionAttributesAndValues,
                        ActionAttributesAndValues = x.ActionAttributesAndValues,
                        CommentOrNotes = x.CommentOrNotes,
                        MigrationDate = x.MigrationDate,
                        CreatedDate = x.CreatedDate,
                        SubmitterLastNameFirstName = x.SubmitterLastNameFirstName,
                        CQTicket = x.CQTicket
                    }).ToList();
                }


}
           
            return modelList;
        }

Open in new window

Avatar of lulu50

ASKER

srikanthreddyn143


You are correct !!!!

it's working great

I had a null values in the database that's why I was getting the error.


I added x.CommitID != null &&      and here's the magic it works

 if (model.SelectedCommitID != null)
                    {
                        list = list.Where(x => x.CommitID != null && model.SelectedCommitID.Any(m => Array.IndexOf(x.CommitID.Split(','), m) > -1));
                    }
Avatar of lulu50

ASKER

Thank you for taking the time to help me

Thank you so much.

Thank you

one more

Thank you