asp.net修改FindAll中的列表或Where而不修改搜索的原始列表

时间:2016-08-03 17:34:00

标签: asp.net linq list where findall

我正在尝试从findall或where创建一个新列表并修改该新列表,而不对其进行更改。从FindAll创建list2并对list2进行更改时,它不应该影响list1。怎么会?

编辑...两个答案的组合使用似乎正在起作用。任何预见的问题???工作代码已编辑 编辑......它不起作用。作为对象属性的类没有复制其数据......任何想法???我不明白为什么复制或克隆对于列表来说不简单明了。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Started");
        List<SampleData> list1 = SampleData.MakeList(3);
        for (int i = 0; i < list1.Count; i++)
        {
            Console.WriteLine("list1.Number => " + list1[i].Number);
            Console.WriteLine("list1.Person => " + list1[i].Person);
        }
        List<SampleData> list2 = new List<SampleData>(list1.FindAll(m => m.Number == 1).Select(x => new SampleData().ShallowCopy())).ToList();
        for (int i = 0; i < list2.Count; i++)
        {
            list2[i].Person = "Person " + list2[i].Number;
            Console.WriteLine("list2.Number => " + list2[i].Number);
            Console.WriteLine("list2.Person => " + list2[i].Person);
        }

        SampleData sd = list2.Find(s => s.Number == 1);
        Console.WriteLine("Apartment Number" + sd.apartment.BuildingLetter); // <<<<------------------- THROWING NULL EXCEPTION FOR APARTMENT

        Console.WriteLine("-------AFTER MODIFYING NEW LIST----LIST1.Person SHOULD BE NULL---");
        for (int i = 0; i < list1.Count; i++)
        {
            Console.WriteLine("--list1.Number => " + list1[i].Number);
            Console.WriteLine("--list1.Person => " + list1[i].Person);
        }
        for (int i = 0; i < list2.Count; i++)
        {
            Console.WriteLine("--list2.Number => " + list2[i].Number);
            Console.WriteLine("--list2.Person => " + list2[i].Person);
        }
        Console.ReadLine();
        System.Environment.Exit(0);
    }

}
class SampleData
{      
    public int Number { get; set; }
    public String Person { get; set; }
    public Apartment apartment { get; set; }
    public static List<SampleData> MakeList(int count)
    {
        List<SampleData> l = new List<SampleData>();
        for (int i = 0; i < count; i++)
        {
            SampleData d = new SampleData();
            d.apartment = new Apartment { RoomNumber = i, BuildingLetter = "letter-" + i };
            d.Number = i;
            l.Add(d);
        }
        return l;
    }
    public SampleData ShallowCopy()
    {
        return (SampleData)this.MemberwiseClone();
    }

}
class Apartment
{
    public int RoomNumber { get; set; }
    public String BuildingLetter { get; set; }
}

2 个答案:

答案 0 :(得分:0)

您可以使用。 AsNoTracking() 实体查询的方法选择 DbContext ObjectContext

List<SampleData> list2 = new List<SampleData>(list1.Where(m => m.Number == 1).AsNoTracking().ToList());

或使用像这样的复制承包商

class SampleData
{
    public int Number { get; set; }
    public String Person { get; set; }

    public SampleData(SampleData obj)
    {
        This.Number=obj.Number;
        This.Person=obj.Person;
        ..
        ..
    }
    public static List<SampleData> MakeList(int count)
    {
        List<SampleData> l = new List<SampleData>();
        for (int i = 0; i < count; i++)
        {
            SampleData d = new SampleData();
            d.Number = i;
            l.Add(d);
        }
        return l;
    }
}

然后

 List<SampleData> list2 = new List<SampleData>(list1.Where(m => m.Number == 1).Select(x=>new SampleData(x)).ToList());

答案 1 :(得分:0)

ShallowCopy是你的朋友。

在SampleData类中定义一个ShallowCopy()方法,如下所示。

class SampleData
{
    public int Number { get; set; }
    public String Person { get; set; }
    public static List<SampleData> MakeList(int count)
    {
        List<SampleData> l = new List<SampleData>();
        for (int i = 0; i < count; i++)
        {
            SampleData d = new SampleData();
            d.Number = i;
            l.Add(d);
        }
        return l;
    }

    public SampleData ShallowCopy()
    {
        return (SampleData)this.MemberwiseClone();
    }

}

然后按如下方式填充list2 ......

List<SampleData> list2 = new List<SampleData>();
foreach (SampleData sd in list1.Where(m => m.Number == 1))
{
    list2.Add(sd.ShallowCopy());
}