c# - 使用文本文件中的三个项目的批处理命令

标签 c# linq

我有一个文本文件,如下所示:

Tokyo 
Japan
8797987
Amsterdam
Nederland
88788787
Den Haag 
Nederland      
787875454
Madrid
Spain
7877878

所以批处理中有三项: 首都 国家 人口

我正在使用 MoreLinq 中的 Batch 命令。

我知道用字典来做。

但是如何管理每批处理三个项目?

我的意思是,例如您正在搜索一个首都和国家,您将返回首都+国家+人口

我尝试这样:

public interface IDatabase
    {

        int GetPopulation(string name);

    }

    public class SingleTOnDatabase : IDatabase
    {

        private System.Collections.Generic.List capitols;

        private SingleTOnDatabase()
        {
            Console.WriteLine("Initializing database");

            capitols = File.ReadAllLines("Capitols.txt")
               .Batch(3)
                .ToList(
                list => list.ElementAt(0).Trim(),
                list => list.ElementAt(1).Trim(),
                list => int.Parse((list.ElementAt(2)))
                );
        }

        public int GetPopulation(string name)
        {

            return capitols[name];

        }

        private static Lazy<SingleTOnDatabase> instance = new Lazy<SingleTOnDatabase>(() => new SingleTOnDatabase());
        public static SingleTOnDatabase Instance => instance.Value;

    }
    public class Program
    {
         static void Main(string[] args)
    {

        var db = SingleTOnDatabase.Instance;
        var city = "Den Haag";
        var Country = "Nederland";
        Console.WriteLine($"{Country} with {city} has population of: {db.GetPopulation(city)}");
        Console.Read();


    }
    }

最佳答案

您永远不应该尝试使用文本文件作为数据库(如果这是一项严肃的工作,对于关心的业余爱好项目来说)。

我修改了您的“批处理”加上 GetPopulation(还添加了 GetCapitol):

public interface IDatabase
{
    int? GetPopulation(string name);
    Capitol GetCapitol(string name);
}

public class Capitol
{ 
    public string CapitolName { get; set; }
    public string Country { get; set; }
    public int? Population { get; set; }
}

public class SingleTOnDatabase : IDatabase
{

    private System.Collections.Generic.List<Capitol> capitols;

    private SingleTOnDatabase()
    {
        Console.WriteLine("Initializing database");

        int pop;
        capitols = (from batch in File.ReadAllLines("Capitols.txt").Batch(3)
                       let bArr = batch.ToArray()
                       where bArr.Length == 3
                       select new Capitol
                       {
                           CapitolName = bArr[0].Trim(),
                           Country = bArr[1].Trim(),
                           Population = int.TryParse(bArr[2], out pop) ? pop : (int?)null
                       }).ToList();
    }


    public int? GetPopulation(string name)
    {
        var capitol = GetCapitol(name);
        return capitol?.Population;
    }

    public Capitol GetCapitol(string name)
    {
        return capitols.SingleOrDefault(c => c.CapitolName.ToLower().Trim() == name.ToLower().Trim());
    }

    private static Lazy<SingleTOnDatabase> instance = new Lazy<SingleTOnDatabase>(() => new SingleTOnDatabase());
    public static SingleTOnDatabase Instance => instance.Value;

}
public class Program
{
    static void Main(string[] args)
    {

        var db = SingleTOnDatabase.Instance;
        var city = "Den Haag";
        var Country = "Nederland";
        Console.WriteLine($"{Country} with {city} has population of: {db.GetPopulation(city)}");

        var city2 = "Tokyo";
        var cap = db.GetCapitol(city2);
        if (cap == null)
        {
            Console.WriteLine($"Unknown city [{city2}].");
        }
        else
        {
            Console.WriteLine($"{cap.CapitolName} is the capital of {cap.Country} and has population of: {cap.Population}");
        }

        Console.Read();
    }
}

注意:根据顶部给定的示例文本,这是我得到的输出:

Initializing database
Nederland with Den Haag has population of: 787875454
Tokyo is the capital of Japan and has population of: 8797987

关于c# - 使用文本文件中的三个项目的批处理命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017337/

相关文章:

c# - ORA-01790 与 EntityFramework

c# - Linq 查询 Group By 和 contains

c# - 如何在 Repository 中实现通用方法以在 linq 中进行联接

c# - .Net 中 LINQ 和 Lambda 表达式的效率和性能如何?

c# - DPI 未正确缩放

c# - 如何在 C# Winforms 中的下拉列表控件中获取数据源名称

c# - 在 XNA 中创建二维多边形

c# - 奇怪的可空比较行为

c# - LINQ 性能常见问题

c# - 查找无法更改密码的用户