C# C如何在两个文本文件之间建立连接
我有两个文本文件读入两个不同的列表 txt包含ID和狗名,如下所示: 第二个.txt文件再次包含狗的ID和类型: 问题是:如何将相同ID的名称和类型写入控制台 守则:C# C如何在两个文本文件之间建立连接,c#,list,text-files,C#,List,Text Files,我有两个文本文件读入两个不同的列表 txt包含ID和狗名,如下所示: 第二个.txt文件再次包含狗的ID和类型: 问题是:如何将相同ID的名称和类型写入控制台 守则: class Program { static void Main(string[] args) { StreamReader sr = new StreamReader("dognames.txt", Encoding.Default); List<Name
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader("dognames.txt", Encoding.Default);
List<Names> Dog_names = new List<Names>();
string header = Olvas.ReadLine();
while (!sr.EndOfStream)
{
Dog_names.Add(new Names(Olvas.ReadLine()));
}
sr.Close();
StreamReader sr2 = new StreamReader("dogtypes.txt", Encoding.Default);
List<Types> Dog_types = new List<Types>();
string header = Olvas.ReadLine();
while (!sr2.EndOfStream)
{
Dog_types.Add(new Types(Olvas.ReadLine()));
}
sr2.Close();
}
}
class Names
{
public int ID;
public string Name;
public Names(string DateLine)
{
string[] DateLineElements = DateLine.Split(';');
this.ID = int.Parse(DateLineElements[0]);
this.Name = DateLineElements[1];
}
}
class Types
{
public int ID;
public string Dog_Type;
public Types(string DateLine)
{
string[] DateLineElements = DateLine.Split(';');
this.ID = int.Parse(DateLineElements[0]);
this.Dog_Type = DateLineElements[1];
}
}
}您可能需要linq加入。。我将使用查询语法,因为我发现它对于连接更具可读性:
var j = from n in Dog_names
join t in Dog_types on n.ID = t.ID
select new {
n.Name,
t.Dog_Type
};
foreach(var d in j)
Console.WriteLine($"Dog {d.Name} is a {d.Dog_Type}");
如果您想了解方法语法,那么有很多关于LINQ Join的教程,例如好的,首先您已经将非常简单的任务复杂化了。 有多种解决方案 这是一种可能性 为此创建一个模型,如:
class Dog
{
public int Id {get; set;}
public string Name {get; set;}
public string DogType {get; set;} //it could be and even should be an enum
}
接下来,只分析每一行,例如:
class DogParser
{
public Dog CreateDog(string line)
{
Dog dog = new Dog();
string[] fields = line.Split(';');
dog.Id = Convert.ToInt32(fields[0]);
dog.Name = fields[1];
return dog;
}
public void UpdateDog(string line)
{
string[] fields = line.Split(';');
int dogId = Convert.ToInt32(fields[0]);
Dog dog = SomehowGetDogById(dogId);
dog.DogType = fields[0];
}
}
现在,只需将这些文件读入不同的列表对象。并对它们进行迭代。首先,使用名称迭代dogs,并使用DogParser中的CreateDog方法。把每只狗都保存在一些列表中,或者更好的字典中,其中的键应该是狗的id
然后迭代第二个列表,并在方法SomehowGetDogById中从字典返回dog
此解决方案不完整且容易出错。所以你必须升级到你的需要
正如我所说,这只是一个循序渐进的例子。有很多解决方案。另一种方法是使用一些csv解析器,例如,您可以读取并尝试Join LINQ方法:
// Sample definitions
class Types { public int ID; public string Type; }
class Names { public int ID; public string Name; }
// Implementation
var types = new List<Types>();
var names = new List<Names>();
var joinedResults = names.Join(types, n => n.ID, t => t.ID, (name, type) => new { name.Name, type.Type });
这些类也许应该被称为DogName和DogType——它们至少不应该有复数名。。
// Sample definitions
class Types { public int ID; public string Type; }
class Names { public int ID; public string Name; }
// Implementation
var types = new List<Types>();
var names = new List<Names>();
var joinedResults = names.Join(types, n => n.ID, t => t.ID, (name, type) => new { name.Name, type.Type });