LINQ词典中的Where-Clause不起作用

时间:2017-05-30 18:40:54

标签: c# linq

我真的无法解决这个问题。我有一个字典,我已经保存了一个编码表。我现在正在尝试获取密钥的正确值。 具有那些不同where子句的if子句应该100%导致结果,但它不会。 where-clause是否被忽略?

public static string ByteArrayToHex(byte[] bytes)
    {            
            StringBuilder Result = new StringBuilder(bytes.Length * 2);
            string HexAlphabet = "0123456789ABCDEF";

            foreach (byte B in bytes)
            {
                Result.Append(HexAlphabet[(int)(B >> 4)]);
                Result.Append(HexAlphabet[(int)(B & 0xF)]);
            }

            return Result.ToString();

    }


static void Main(string[] args)
    {
        List<byte> byteList2 = new List<byte>() { 227, 129,130 };

        List<byte> byteList = new List<byte>() {131, 95, 131, 126, 129, 91};
        string filename = "file.txt";
        var dict = Shared.IO.Read.EncodingTable(filename);

        var original = Encoding.GetEncoding(932).GetString( byteList.ToArray());
        var custom = CustomConverter(byteList, dict);

        var original2 = Encoding.GetEncoding(932).GetString(byteList2.ToArray());
        var custom2 = CustomConverter(byteList2, dict);

        Console.WriteLine("Original: " + original);
        Console.WriteLine("Custom: " + custom);
        Console.WriteLine("Original: " + original2);
        Console.WriteLine("Custom: " + custom2);

        Console.ReadKey();
    }

    static string CustomConverter(List<byte> byteList, Dictionary<string,string> dict)
    {
        var result = string.Empty;

        while (byteList.Count != 0)
        {
            var count = byteList.Count;

            byte[] bytes = new byte[] { };
            if (count > 2) bytes = new byte[3] {byteList[0], byteList[1], byteList[2]};
            if (count == 2) bytes = new byte[2] {byteList[0], byteList[1]};
            if (count == 1) bytes = new byte[1] {byteList[0]};

            var hexString = Shared.IO.Convert.ByteArrayToHex(bytes);
            KeyValuePair<string,string> entry;



            if (hexString.Length > 2)
            {
                var a = hexString.Substring(0, 4);
                var b = hexString.Substring(0, 2);
                var c = hexString.Substring(0, 1);

                entry = (from item in dict
                    where item.Key == hexString || item.Key == a || item.Key == b || item.Key == c
                    select item).First();
            }

            else
            {
                var a = hexString.Substring(0, 1);

                entry = (from item in dict
                where item.Key == hexString || item.Key == a
                select item).First();
            }
            byteList.RemoveRange(0, (entry.Key.Length / 2));

            result = result + entry.Value;
        }

        return result;
}`

public static Dictionary<string, string> EncodingTable(string filename)
    {
        string directory = Environment.CurrentDirectory + @"\encoding\";
        string path = directory + filename;

        Dictionary<string,string> encodingTable = new Dictionary<string, string>();

        var lines = System.IO.File.ReadAllLines(path);

        foreach (var line in lines)
        {
            if (string.IsNullOrEmpty(line)) continue;
            if (line.StartsWith("#")) continue;

            var lineArray = line.Split(';');
            encodingTable.Add(lineArray[0], lineArray[1]);
        }

        return encodingTable;
    }`

字典包含这样的内容

79;か 图7A;き 图7B;く 7C;け 7D;こ 7E;さ 7F;し 80;す 81;せ 82;そ 83;た 84;ち

2 个答案:

答案 0 :(得分:0)

给出您的代码,并使用您提供的数据创建一个file.txt

5F;_ 5B;[ E1;# E3;#

我收到回复:

Original: ダミー
Custom: た _た さ せ [
Original: 縺・
Custom: #せ そ 

您是否看到了不同的东西?或者这不是你想要的?

答案 1 :(得分:0)

就我尝试过不同的东西而言,TryGetValue方法是使用太多where-conditions的最佳选择。即使表现非常糟糕。感谢提示&#34; D Stanley&#34;。

目标是检查字典(size = max 3 bytes),如果我当前的数组(转换为hexstring)在此字典中。如果是这种情况,它将返回输出值。希望有人发现它很有用。

    private static string _hexString = null;
    public static string ByteValueToCustomEncoding(byte[] byteArray, Dictionary<string, string> dict)
    {
        var result = string.Empty;

        _hexString = Shared.IO.Convert.ByteArrayToHex(byteArray);

        while (_hexString.Length > 0)
        {
            string entry = string.Empty;

            if (_hexString.Length >= 6)
            {
                entry = Helper(dict);
            }

            else if (_hexString.Length >= 4)
            {
                entry = Helper(dict);
            }

            else if (_hexString.Length >= 2)
            {
                entry = Helper(dict);
            }

            else if (_hexString.Length >= 1)
            {
                entry = Helper(dict);
            }

            result = result + entry;
        }

        return result;
    }

    private static string Helper(Dictionary<string,string> dict)
    {
        string temp = String.Empty;

        for (int i = (_hexString.Length > 6) ? 6: _hexString.Length; i >= 0; i = i - 2)
        {
            if (dict.TryGetValue(_hexString.Substring(0, (i == 0) ? 1 : i), out temp))
            {
                _hexString = _hexString.Remove(0, i);
                return temp;
            }
        }

        _hexString = _hexString.Remove(0, 1);

        return temp;
    }