2

I am trying to combine two LIKE objects together and remove duplicates.

Tried this

This didn't work

Here is my object [simple]

public class LabelItem
{
    public string LabelName { get; set; }
    public string LabelValue { get; set; }
}

my data call returns the same object type

public static List<LabelItem> ReturnControlLabelList(Enums.LanguageType languageType, string labelList = "")

I pass this to the method

string[] LABELLIST = new string[] { "foxLabel", "commonLabel" };
var helper = new LabelHelper(, LABELLIST);

this is where I get null

    public LabelHelper(Enums.LanguageType languageType, string[] labelListName)
    {
        if (labelListName != null)
        {
            List<LabelItem> labels = new List<LabelItem>();
            this.LabelList = new List<LabelItem>();

            foreach (var name in labelListName)
            {
                labels = DBCommon.ReturnControlLabelList(languageType, name);
                this.LabelList.Concat(labels).Distinct().ToList();
            }

        }
        else
        {
            this.LabelList = null;
        }

    }

public List<LabelItem> LabelList { get; private set; }

The concat is not working. I keep getting count 0 for labels and I can see the returns come back with 275 and 125 in the for loop.

Thanks in advance for the help.

Still having an issue

I want to use the suggestion from below but am still struggling.

The string[] passed in will get two lists of labelitems that are not unique when joined together in the loop. I need the distinct of the multiple lists returned in this.LabelList.

I got it to work with this but...I'm sure it's crazy inefficient.

Thanks for the help.

             this.LabelList = new List<LabelItem>();

            foreach (var name in labelListName)
            {
                var ret =  DBCommon.ReturnControlLabelList(languageType, name);
                this.LabelList = this.LabelList.Concat(ret).Distinct().ToList();             
            }

            var distinctList = this.LabelList.GroupBy(x => new  { x.LabelName, x.LabelValue })
                 .Select(x => x.FirstOrDefault());

            this.LabelList = new List<LabelItem>();

            foreach (var item in distinctList)
            {
                this.LabelList.Add(item);
                Debug.WriteLine(item.LabelName + ' '  + item.LabelValue);
            }

        }

2 Answers 2

8

this.LabelList.Concat(labels).Distinct().ToList(); without assigning it to something doesn't make much sense. LINQ query does not modify the source collection, it returns a new one, so you'd have to assign it back to this.LabelList if you want it to get updated:

this.LabelList = this.LabelList.Concat(labels).Distinct().ToList();

You should be aware, that it's highly inefficient solution, and you should go with something based on SelectMany:

this.LabelList
    = labelListName.SelectMany(name => DBCommon.ReturnControlLabelList(languageType, name)
                   .Distinct()
                   .ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

Concat and most other linq methods return an IEnumerable which you then need to do something with. It will not change your existing list so you need to just assign it with:

this.LabelList = this.LabelList.Concat(labels).Distinct().ToList();

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.