对象链

时间:2018-09-29 01:36:50

标签: c# linq

我有一个对象;

class MyObject
{
   double Timestamp;
   string Originalname;
   string Renamedname;
}

我有一个MyObject数组

  

1538148190,“ C:\ test \ jeff.txt”,“ jeffmodified.txt”   1538148200,“ C:\ test \ jeffmodified.txt”,“ jeffmodified2.txt”   1538148289,“ C:\ test \ jeffmodified2.txt”,“ jeffmodified3.txt”   1538149002,“ C:\ test \ thing.txt”,“ something.txt”

我正在尝试创建数据的相关性,以提出将jeff.txt重命名为jeffmodified.tx,然后将其重命名为jeff.modified2.txt和jeffmodified3.txt的想法。时间戳(unix格式)向我显示jeff.txt是原始文件名。

这可以在linq中完成吗?这仅适用于内存中的对象-不适用于数据库,并且对象的最大数量通常为200个对象。

1 个答案:

答案 0 :(得分:0)

Using linq this cannot be done in a one or two liner (if that is what you meant by can this be done in linq. But due to linq the solution does become easier then what it would have been without linq.

The important assumptions that I have made in the solution below is that the timestamp will increase with each file name change. Solution is explained in the comments provided.

   static void Main(string[] args)
        {
            // setting up test data, can be removed from actual solution where objects can be populated as required
            List<MyObject> objects = new List<MyObject>();
            objects = GetData(); 
            int i = 0;
            int key = 0;
            List<WrapMyObject> wObjs = new List<WrapMyObject>();
            Dictionary<int, string> origDictionary = new Dictionary<int, string>();
            Dictionary<int, string> searchDictionary = new Dictionary<int, string>();


            // adding the directory path to the renamed filename as well, since same filename can be there for different filepath
            Func<string, string> GetFilePath = (path) => path.Substring(0, path.LastIndexOf('\\'));
            objects.ForEach(x=>x.Renamedname = GetFilePath(x.Originalname) + "\\" + x.Renamedname);

            //sort the whole list by timestamp, this should ensure that we begin always with the orignal file and always get subsequent changed filename in the list
            //the algorithm relies on the fact that the renamed name should be the next orignal name for that file change in the sorted list 
            //if current item's orignal name is not in search dictory then we are seeing this file path first time, add to orignal dictionary with key, add the renamed name in search dictionary with same key
            //else add add the search path found in searchDictionary back into origDictionary and update the searchDict with the new path value (current item renamed path)
            var sortedObjects = objects.OrderBy(o => o.Timestamp);
            foreach (var item in objects)
            {              
                if (searchDictionary.ContainsValue(item.Originalname))
                    {                    
                        key = searchDictionary.Where(x => x.Value == item.Originalname).First().Key;
                        origDictionary[key] = origDictionary[key] + "!" + searchDictionary[key];
                        searchDictionary[key] = item.Renamedname;
                    }
                    else
                    {
                        origDictionary.Add(i, item.Originalname);
                        searchDictionary.Add(i, item.Renamedname);
                    }
                i++;
            }            
            //iterated the whole list of objects....which means the final node would be remaining in the search dictionary, add them back to origDictionary to complete the modified path
            searchDictionary.ToList().ForEach(x => origDictionary[x.Key] = origDictionary[x.Key] + "!" + searchDictionary[x.Key]);

            //at this point orgnalDict has the list of all names that each file was modified with.
        }