简体   繁体   English

在C#中设置和获取集合

[英]Set and Get a collection in C#

I have two classes: Group and Item. 我有两个班级:组和项目。

public class Group
    {
        public string Name{ get; set; }
        public List<Item> ItemList { get; set; }
    }

And then Item 然后项目

public class Item
    {
        public int ID{ get; set; }
        public string Name{ get; set; }
        public string Description{ get; set; }
        public Group ItemGroup {get;set;}       
      }

Each group show have a set of items. 每个小组表演都有一组项目。

The following code is meant to get the list of items of a particular group, and it works when the ItemGroup in the Items class is set to type string, but not as a type Group. 以下代码用于获取特定组的项目列表,并且在Items类中的ItemGroup设置为字符串类型而不是组类型时有效。

 public IEnumerable<Item> GetItemByGroup(string group)
 {
  return repository.GetAllItems().Where(
                p => string.Equals(p.ItemGroup, group, StringComparison.OrdinalIgnoreCase));
 }

How to change the code to get the list of items in a group by its Name property which is set in the Group class. 如何通过Group类中设置的Name属性更改代码以获取组中项目的列表。

And how do I set a list/collection of Items in the Group class 以及如何在Group类中设置项目的列表/集合

I think this should work. 我认为这应该有效。 Give it a hit 给它一个打击

public IEnumerable<Item> GetItemByGroup(string group)
 {
    return repository.GetAllItems().Where(p =>p.ItemGroup.Name.Equals(group));
 }

Update 更新

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Item> list = new List<Item>();

            Item i = new Item()
            {
                ID = 1,
                Name = "Amit",
                Description = "Test",
                 ItemGroup = new Group() { Name = "A" }
            };
            list.Add(i);
            i = new Item()
           {
               ID = 2,
               Name = "Amit1",
               Description = "Test1",
               ItemGroup = new Group() { Name = "A1" }
           };
            list.Add(i);
            i = new Item()
            {
                ID = 3,
                Name = "Amit11",
                Description = "Test11",
                ItemGroup = new Group() { Name = "A11" }
            };
            list.Add(i);

            i = new Item()
            {
                ID = 4,
                Name = "Amit111",
                Description = "Test111",
                ItemGroup = new Group() { Name = "A111" }
            };
            list.Add(i);
            i = new Item()
            {
                ID = 9,
                Name = "Amit4a",
                Description = "Test4a",
                ItemGroup = new Group() { Name = "A111" }
            };
            list.Add(i);
            i = new Item()
            {
                ID = 5,
                Name = "Amit5",
                Description = "Test5",
                ItemGroup = new Group() { Name = "A111" }
            };
            list.Add(i);
            i = new Item()
            {
                ID = 6,
                Name = "Amit6",
                Description = "Test6",
                ItemGroup = new Group() { Name = "A111" }
            };
            list.Add(i);

            var list1 = list.Where(p => p.ItemGroup.Name.Equals("A111"));
         //   Console.Write(list1.Count());

            foreach (var item in list1)
            {
                 Console.WriteLine(string.Format("ID: {0}  Name: {1}  Description:  {2}  Group: {3}",item.ID,item.Name,item.Description,item.ItemGroup.Name));
            }
            Console.Read();
        }
    }

    public class Group
    {
        public string Name { get; set; }

    }
    public class Item
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public Group ItemGroup { get; set; }

    }
}

在此处输入图片说明

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

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