简体   繁体   English

返回字符串作为自定义对象列表中的属性出现的次数

[英]Return the number of times a string occurs as a property in a list of custom objects

I'm trying to return a list with the number of times a property has a value set. 我正在尝试返回一个列表,其中包含属性设置值的次数。

I'm trying to do this without having to hardcode the value to look for, so if something changes on the backend I won't have to add a new line of code. 我试图这样做而不必硬编码要查找的值,所以如果后端发生变化,我将不必添加新的代码行。

Currently I have it working, but I have set the values manually. 目前我有它工作,但我已手动设置值。

listCounts.Add(testList.Count(item => item.title == "Blah"));
listCounts.Add(testList.Count(item => item.title == null));
listCounts.Add(testListt.Count(item => item.title == "test"));
listCounts.Add(testList.Count(item => item.title == "Blarg"));

This works currently but if anything chaanges, i'll have to go in and made changes to the code which is what I am trying to avoid 这当前工作,但如果有任何chaanges,我将不得不进入并更改代码,这是我试图避免

Depends on what you're trying to do really. 取决于你真正想做的事情。 It looks like you want the count of wach of those keys (the titles)? 看起来你想要那些键(标题)的wach计数?

One way would be to group by your title to give the counts, eg. 一种方法是按标题分组以给出计数,例如。

var listCounts = testList.GroupBy(item => item.title);

As an example of using this: 作为使用它的一个例子:

    class Item
    {
        public string title;
    }

    static void Main(string[] args)
    {
        var testList = new List<Item>
        {
            new Item { title = "Blah" },
            new Item { title = "Blah" },
            new Item { title = "Blah" },
            new Item { title = null },
            new Item { title = null },
            new Item { title = "test" },
            new Item { title = "test" },
            new Item { title = "test" },
            new Item { title = "test" }
        };


        var listCounts = testList.GroupBy(item => item.title);

        foreach (var count in listCounts)
        {
            Console.WriteLine("{0}: {1}", count.Key ?? string.Empty, count.Count());
        }

        Console.ReadKey();

    }

The disadvantage is you're getting the count each time - like I said, it depends on what you're trying to achieve. 缺点是你每次都得到计数 - 就像我说的,这取决于你想要达到的目标。 A simple change would make this a dicationary (string, int), whereby each title would be a key, and the value would be the number of times the title appears. 一个简单的改变会使它成为一个dicalary(string,int),每个标题都是一个键,值将是标题出现的次数。

EDIT 编辑

To use a dictionary, change the listCounts line to: 要使用字典,请将listCounts行更改为:

var listCounts = testList.GroupBy(t => t.title).ToDictionary(i => i.Key ?? string.Empty, i => i.Count()); 

(note that a key cannot be null, hence the i.Key ?? string.Empty workaround, should be fine for your purposes) (注意一个键不能为null,因此i.Key ?? string.Empty变通方法,应该没问题)

We don't know what your backend is, but it would seem you need to retrieve the values from it. 我们不知道你的后端是什么,但似乎你需要从中检索它们。

//string[] myStrings = new string[] { "Blah", null, "test", "Blarg" };
string[] myStrings = _backEnd.RetrieveValues();
listCounts.Add(testList.Count(item => myStrings.Contains(item)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM