2

我正在尝试从具有与用户输入匹配的列值的数据表中获取行值。

我找到了这个链接:

如何从数据表中获取特定的列值?

但我不确定,因为我想能够说

where userID =@userinput and DateBooked =@DateBooked and 
RoomQty=@RoomQty and NoofNights=@NoofNights

.

这个例子适用于我的情况吗?

4

2 回答 2

2

您应该能够按照提供的链接中的示例进行操作。

假设你有一个像这样的 DataTable

            DataTable dt = new DataTable();
            dt.Columns.Add("UserID");
            dt.Columns.Add("FullName");
            dt.Columns.Add("DateBooked");
            dt.Columns.Add("RoomQty");
            dt.Columns.Add("NoofNights");

            dt.Rows.Add(1, "John Doe", DateTime.Now.AddDays(-7).Date, 2, 3);
            dt.Rows.Add(2, "Jack Sparrow", DateTime.Now.AddDays(-14).Date, 1, 1);
            dt.Rows.Add(3, "Arthur Wilson", DateTime.Now.AddDays(-10).Date, 2, 2);
            dt.Rows.Add(4, "Amy Jackson", DateTime.Now.AddDays(-5).Date, 4, 2);
            dt.Rows.Add(5, "Tim Lee", DateTime.Now.AddDays(-1).Date, 1, 5);
            dt.Rows.Add(2, "Jack Sparrow", DateTime.Now.AddDays(-5).Date, 3, 2);

你的输入参数是

            int userInput = 2;
            DateTime dateBooked = DateTime.Now.AddDays(-14).Date;
            int roomQty = 1;
            int noOfNights = 1;

您可以'FullName'像这样获取列值(前提是您将根据您的 where 条件拥有一条记录)

            string FullName = (from DataRow dr in dt.Rows
                               where Convert.ToInt32(dr["UserID"]) == userInput &&
                               Convert.ToDateTime(dr["DateBooked"]) == dateBooked &&
                               Convert.ToInt32(dr["RoomQty"]) == roomQty &&
                               Convert.ToInt32(dr["NoofNights"]) == noOfNights
                               select (string)dr["FullName"]).FirstOrDefault();
于 2012-05-14T01:29:20.213 回答
0
IEnumerable<String> userNames =
    dataTable
    .AsEnumerable()
    .Where(row =>
        row.Field<Int32>("userID") == userinput &&
        row.Field<DateTime>("DateBooked") == dateBooked &&
        row.Field<Int32>("RoomQty") == roomQty &&
        row.Field<Int32>("NoofNights") == noofNights)
    .Select(row => row.Field<String>("userName"));
于 2014-08-28T08:14:03.893 回答