如何匹配c#中不同方法的信息

时间:2015-08-06 16:59:21

标签: c# methods matching

我正在编写一个程序,该程序将从我创建的方法中获取信息,然后向用户写入他们收集的数据的电子邮件。他们收集的数据是试图登录或注册的客户试图进入移动应用程序的错误消息。以下是我在程序主要方法中要说的内容:

foreach(Transaction event in recentFailedTransactions)
{
  if(!TransactionSuccessFound(transaction))
    {
       //Match the response data from TransactionFailure with information from myhome.ToString();
    }

所以我的主要问题是:从我开发的两种方法中,.NET中是否有一个功能可以让我将这两种方法结合起来,以便将数据转换为字符串?或者我将不得不重新开发我的方法?以下是我从中接收数据的方法。失败方法收集所有错误数据,myhome方法收集所需的所有用户信息。任何帮助表示赞赏!

//failure method
private static List<TransactionInformation> TransactionFailures(DateTime lastRanTime)
    {
  List<TransactionInformation> recentFailedTransactions = new List<TransactionInformation>();
  using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.MOBILEConnectionString))
    {
    SqlCommand sqlError = connection.CreateCommand();
    sqlError.CommandText = @"SELECT SERVICE_ID, REQUEST_DATA, RESPONSE_DATA, DATE_ENTERED FROM WS_TRANSACTION WHERE SERVICE_ID IN ('GetUserTokenLogin', 'RegisterUser') AND RESPONSE_DATA LIKE '%false%' ORDER BY DATE_ENTERED DESC";
    connection.Open();  
    SqlDataReader reader = sqlError.ExecuteReader();

    while (reader.Read())
      {
       List<string> transactionElementList = new List<string>();
       transactionElementList.Add(reader.GetString(0));

       foreach (string line in transactionElementList)
       {
         TransactionInformation fail = new TransactionInformation();
         fail.ServiceID = Utilities.FromDBValue<string>(reader["SERVICE_ID"]);
         fail.RequestData = Utilities.FromDBValue<string>(reader["REQUEST_DATA"]);
         fail.ResponseData = Utilities.FromDBValue<string>(reader["RESPONSE_DATA"]);
         fail.TransactionDate = Utilities.FromDBValue<DateTime>(reader["DATE_ENTERED"]);
         string[] exception = fail.ResponseData.Split(new[] { '{', '"', ':', '}' }).Skip(1).ToArray();
         fail.DisplayException = exception[0 + 1];
         fail.InternalException = exception[2 + 3];


          switch (Utilities.FromDBValue<string>(reader["SERVICE_ID"]))
           {
           case "GetUserTokenLogin":
           string[] login = line.Split(new[] { '[', '|', ']' }).Skip(1).ToArray();

            fail.Type = TransactionInformation.TransactionType.Login;
            fail.UserName = login[0];
            break;
            case "RegisterUser":
            string[] parts = line.Split(new[] { '[', ']' }).Skip(1).ToArray();

            fail.Type = TransactionInformation.TransactionType.Registration;
            fail.UserName = parts[0];
            fail.Email = parts[1];
            fail.FirstName = parts[2];
            fail.LastName = parts[3];
            fail.PNRLocator = parts[4];
            break;
            default:
            throw new Exception("Unknown service id");
           }
          recentFailedTransactions.Add(fail);
        }

       }
     }
    return recentFailedTransactions;
  }

      //MyHome method
      private static List<MyHome> GetUserDataFromMyHome(string username)
    {
        List<MyHome> myHomeInformation = new List<MyHome>();
        using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.MyHomeConnectionString))
        {
            SqlCommand sqlError = connection.CreateCommand();
            sqlError.CommandText = @"SELECT USER_NAME, EMAIL, FIRST_NAME, LAST_NAME, TRAVELER_UID FROM TANDT_PORTAL.dbo.[USER]";
            connection.Open();
            SqlDataReader reader = sqlError.ExecuteReader();

            while (reader.Read())
            {
                MyHome userInformation = new MyHome();
                foreach (MyHome item in myHomeInformation)
                {
                    userInformation.myHomeUserName = Utilities.FromDBValue<string>(reader["USER_NAME"]);
                    userInformation.myHomeEmail = Utilities.FromDBValue<string>(reader["EMAIL"]);
                    userInformation.myHomeFirstName = Utilities.FromDBValue<string>(reader["FIRST_NAME"]);
                    userInformation.myHomeLastName = Utilities.FromDBValue<string>(reader["LAST_NAME"]);
                    userInformation.myHomeTravelerUID = Utilities.FromDBValue<string>(reader["TRAVELER_UID"]);
                    myHomeInformation.Add(userInformation);
                }
            }
        }
        return myHomeInformation;
    }

1 个答案:

答案 0 :(得分:0)

我仍然不完全明白你想要做什么,但我希望你能利用这一点。如果它们具有相同的用户名,则会将MyClassTransactionInformation对象匹配在一起,并生成包含匹配对象的匿名类型列表。看看:

    private static void JoinSample()
    {
        List<TransactionInformation> failures = ...
        List<MyHome> myHomeData = ...

        var joined =
            from f in failures // Iterate failures
            join d in myHomeData on // Join with myHomeData
            f.Username equals d.MyHomeUserName // when the username matches
            select new // store values in anonymous type
            {
                MyHome = d,
                Failure = f
            };

        foreach (var item in joined)
        {
            Console.WriteLine(item.MyHome.ToString());
            Console.WriteLine(item.Failure.ToString());
        }
    }