0

我有两个类:Group 和 Item。

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 设置为 string 类型而不是 Group 类型时,它可以工作。

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

如何更改代码以通过Name在 Group 类中设置的属性获取组中的项目列表。

以及如何在 Group 类中设置项目列表/集合

4

1 回答 1

1

我认为这应该有效。给它一个打击

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; }

    }
}

在此处输入图像描述

于 2013-10-25T18:41:46.630 回答