簡體   English   中英

在C#中設置和獲取集合

[英]Set and Get a collection in C#

我有兩個班級:組和項目。

public class Group
    {
        public string Name{ get; set; }
        public List<Item> ItemList { 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;}       
      }

每個小組表演都有一組項目。

以下代碼用於獲取特定組的項目列表,並且在Items類中的ItemGroup設置為字符串類型而不是組類型時有效。

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

如何通過Group類中設置的Name屬性更改代碼以獲取組中項目的列表。

以及如何在Group類中設置項目的列表/集合

我認為這應該有效。 給它一個打擊

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

更新

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