I have little problem checking and storing new data from an old one. I have a class structured like this :
public class class1
{
public List<Class2> class2List {get,set}
...
}
public class class2
{
public Class3 class3 {get,set}
...
}
public class class3
{
public string name {get,set}
public int index {get,set}
...
}
I want to create a method that will take a class 2 input and check if indexes are the same as old class1, if yes i want to store it. I try to do sme linQ to solve my problem but I am not an expert and I am a little stuck...
public static void SaveNewDataOnlyIfSameIndex(class1 workingData, List<class2> newdata)
{
List<class3> LocalWorkingDataClass3 = new List<class3>();
foreach(class2 item in workingData.class2)
{
LocalWorkingDataClass3.add(item.class3);
}
List<int> LocalWorkingDataIndex = new List<int>();
foreach(class3 item2 in LocalWorkingDataClass3 )
{
LocalWorkingDataIndex.add(item2.index);
}
List<class3> LocalEqualClasstoReplace = new List<class3>();
LocalEqualClasstoReplace = newdata.Where(File => LocalWorkingDataIndex.Equals(File.Index));
}
CodePudding user response:
i can't understand very well what you need, but you can try to do something like this, using Linq:
public static void SaveNewDataOnlyIfSameIndex(Class1 WorkingData, List<Class2> Newdata)
{
List<int> LocalWorkingDataClass3Index = WorkingData.Class2List.Select(i => i.Class3.Index).ToList()
List<Class3> LocalEqualClasstoReplace = new List<Class3>();
LocalEqualClasstoReplace = newdata.Where(f => LocalWorkingDataClass3Index.Contains(f.Class3.Index));
}
CodePudding user response:
Hi. First of all, I am not sure that i got your question.
public class Class1
{
public List<Class2> class2List {get;set;}
}
public class Class2
{
public string name {get;set;}
public int index {get;set;}
}
public void CompareAndReplace(ref Class1 orginalDataList, Class2 newData){
var check=orginalDataList.class2List.FirstOrDefault(g=>g.index==newData.index);
if(check!=null){
check.name=newData.name;
}
}
//example to use assuming to "orginalDataList" has some datas..
CompareAndReplace(ref orginalDataList,new Class2{index=1,name="Hello"});