We help IT Professionals succeed at work.

Split data then list them in the Dropdown list

lulu50
lulu50 asked
on
Hi,

I have a dropdown list that list the following data like this. This is the output that I have right now.

<select name="CommitIDListID" class="btn btn-default dropdown-toggle" id="CommitIDListID" multiple="multiple">
    <option value="345456456, 553564646">345456456, 553564646</option>
    <option value="353434534">353434534</option>
    <option value="535443335, 756555666, 755444566">535443335, 756555666, 755444566</option>
    <option value="577543332">577543332</option>
    <option value="755444566">755444566</option>
 </select>

Open in new window


...but I want the output to be like this

<select name="CommitIDListID" class="btn btn-default dropdown-toggle" id="CommitIDListID" multiple="multiple">
    <option value="553564646">553564646</option>
    <option value="345456456">345456456</option>
    <option value="353434534">353434534</option>
    <option value="535443335">535443335</option>
    <option value="577543332">577543332</option>
    <option value="755444566">755444566</option>   // my data will be distinct because I have two records of the same commit ID
    <option value="756555666">756555666</option>
</select>

Open in new window


This is the code that I have right now:

<select multiple="multiple" id="CommitIDListID" name="CommitIDListID" class="btn btn-default dropdown-toggle">
                                @foreach (var data in Model.CommitIDList)
                                {
                                    if (data.Selected)
                                    {
                                        <option value="@data.Value" selected="selected">@data.Text</option>
                                    }
                                    else
                                    {
                                        <option value="@data.Value">@data.Text</option>
                                    }
                                }
                            </select>


  public IEnumerable<SelectListItem> GetAllCommitIDList()
        {
            var GetCommitID = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().GroupBy(x => x.CommitID).Select(y => y.First()).Distinct();

            return GetCommitID
                 .Select(x => new SelectListItem
                 {
                     Text = x.CommitID,
                     Value = x.CommitID.ToString(),
                 })
                .Distinct()
                .OrderBy(x => x.Text);

}

Open in new window



This is the code that I'm working on; I know my syntax is wrong:

var CommitIDData = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().Select(g => g.CommitID = string.Join(", ", g.CommitID)).Distinct();
         
   return CommitIDData 
                .Select(g => new SelectListItem
                {
                    Text = g.CommitID,
                    Value = g.CommitID.ToString(),
                })
                 .Distinct()
                .OrderBy(g => g.Text);

Open in new window



My table looks like this: table "RuleDetail"

RuleDetailId    Version         Environment     CQTicket        CommitID
16              Version33	    SIT	            ENT19293	    353434534
17              Version22	    SIT	            ENT95837	    345456456, 553564646
18              version 5	    SIT	            ENT8374	        535443335, 756555666, 755444566
19              version 10	    SIT	            ENT89484	    577543332
20              version25	    SIT	            CQ ticket 55	755444566
21              Version11	    SIT	            ENT00222222

Open in new window



Thank you so much for your help,
Comment
Watch Question

Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
You would split your commits before selecting the items; e.g. -
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 });
}

Open in new window

Proof of concept -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29174412
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var item in GetAllCommitIds())
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }

        static IEnumerable<SelectListItem> GetAllCommitIds()
        {
            var commits = new[]
            {
                new RuleDetail { RuleDetailId = 16, Version = "Version33", Environment = "SIT", CQTicket = "ENT19293", CommitId = "353434534" },
                new RuleDetail { RuleDetailId = 17, Version = "Version22", Environment = "SIT", CQTicket = "ENT95837", CommitId = "345456456, 553564646" },
                new RuleDetail { RuleDetailId = 18, Version = "Version 5", Environment = "SIT", CQTicket = "ENT8374", CommitId = "535443335, 756555666, 755444566" },
                new RuleDetail { RuleDetailId = 19, Version = "Version 10", Environment = "SIT", CQTicket = "ENT89484", CommitId = "577543332" },
                new RuleDetail { RuleDetailId = 20, Version = "Version25", Environment = "SIT", CQTicket = "CQ ticket 55", CommitId = "755444566" },
                new RuleDetail { RuleDetailId = 21, Version = "Version11", Environment = "SIT", CQTicket = "ENT00222222" }
            };

            return string.Join(", ", commits.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 });
        }
    }

    class SelectListItem
    {
        public string Text { get; set; }
        public string Value { get; set; }

        public override string ToString()
        {
            return $"{{ Text: {Text}, Value: {Value} }}";
        }
    }

    class RuleDetail
    {
        public int RuleDetailId { get; set; }
        public string Version { get; set; }
        public string Environment { get; set; }
        public string CQTicket { get; set; }
        public string CommitId { get; set; }
    }
}

Open in new window

Produces the following results -Capture.PNG-saige-
lulu50Web application

Author

Commented:
saige,

Thank you so much!!!!

I'm learning from you.

Thank you so much!!!!!!

You are the best !!!!!!

Thank you again.
lulu50Web application

Author

Commented:
saige,

YOU ARE THE BEST MY FRIEND AND I THANK YOU FROM ALL MY HEART FOR ALL YOUR HELP !!!

Thank you,
Lulu