我的数据库包含2条记录。所以,我检索这两个记录并存储在数组中。假设我的数据库有start point
,destination
,terminal
和departure time
。如果用户尝试添加same start
点,相同destination
,相同terminal
和相同departure time
,则会显示错误消息。但是,如果用户尝试添加相同的start point
,相同的destination
,相同的terminal
但不同的departure time
。它假设允许用户添加,但我有用户不允许添加的问题。
示例:
这些记录在我的数据库中
Record 1 : Terminal = 1 , From = US , To = UK, Time = 10.00
Record 2 : Terminal = 1 , From = US , To = AUS , Time = 2.00
当用户想要添加新记录时
用户希望添加:Terminal = 1, From = US, To = AUS, Time = 10.00
告诉我记录存在,因为时间10.00存在于不同目的地的其他记录中。有人可以帮忙吗?
if (ow_terminal.Any(s => ddlterminal.SelectedValue.Contains(s))
&& ow_depart.Any(s => ddlDeparture.SelectedValue.Contains(s))
&& ow_origin.Any(s => ddlOrigin.SelectedValue.Contains(s))
&& ow_destination.Any(s => ddlDestination.SelectedValue.Contains(s)))
{
lblMessage.Text = "Record exist";
}
else
{
lblMessage.Text = "No record exist"
}
答案 0 :(得分:0)
问题在于您的Linq声明。
从逻辑上思考它实际上在说什么:
if(terminal_array.Any(s => new_terminal_record.Contains(s)))
因此,如果您的terminal_array中的任何值与用户输入的新终端记录匹配,则此语句为true,然后您的代码将评估下一行
if(depart_array.Any(s => new_depart_record.Contains(s)))
它会做同样的事情,如果您的离开数组中的任何值与用户输入的新离开记录相匹配,那么此陈述也是如此。
您最想要做的是将记录存储在任何记录类型的数组中,并对该数组中的每条记录执行Linq语句,类似于:
if(records_array.Any(s => new_record.terminal.Equals(s.terminal) &&
new_record.depart.Equals(s.depart) &&
new_record.origin.Equals(s.origin) &&
new_record.destination.Equals(s.destination)))
{
// Record Exists
}
else
{
// No Record Exists
}
有关更完整的示例,我已复制您的实施,然后包含我的实施:
class Program
{
// Example of database records
struct _dbTypes
{
public string terminal;
public string depart;
public string origin;
public string destination;
};
static void Main(string[] args)
{
// First record
_dbTypes record1;
record1.terminal = "1";
record1.depart = "10.00";
record1.origin = "US";
record1.destination = "UK";
// Second record
_dbTypes record2;
record2.terminal = "1";
record2.depart = "2.00";
record2.origin = "US";
record2.destination = "AUS";
// Your implementation has each of the records internals
// separated into their own array
List<String> terminals = new List<string>();
List<String> departs = new List<string>();
List<String> origins = new List<string>();
List<String> destination = new List<string>();
terminals.Add(record1.terminal);
terminals.Add(record2.terminal);
departs.Add(record1.depart);
departs.Add(record2.depart);
origins.Add(record1.origin);
origins.Add(record2.origin);
destination.Add(record1.destination);
destination.Add(record2.destination);
// The NEW record that the user has entered
_dbTypes record3;
record3.terminal = "1";
record3.depart = "10.00";
record3.origin = "US";
record3.destination = "AUS";
// Example of your implementation
if (terminals.Any(s => record3.terminal.Contains(s)) &&
departs.Any(s => record3.depart.Contains(s)) &&
origins.Any(s => record3.origin.Contains(s)) &&
destination.Any(s => record3.destination.Contains(s)))
{
// Will fall into here
Console.WriteLine("Record exists");
}
else
{
Console.WriteLine("No record exists");
}
// What you should probably be doing...
//
// Array of your record type
List<_dbTypes> dataBaseEntries = new List<_dbTypes>();
//
// Add the first two records as a whole record, not separated out
dataBaseEntries.Add(record1);
dataBaseEntries.Add(record2);
// Now you want to do the Linq statement over the record
// AND'ing each .Equals on the current records internal objects
if (dataBaseEntries.Any(s => record3.terminal.Equals(s.terminal) &&
record3.depart.Equals(s.depart) &&
record3.origin.Equals(s.origin) &&
record3.destination.Equals(s.destination)))
{
Console.WriteLine("Record exists");
}
else
{
Console.WriteLine("No record exists");
dataBaseEntries.Add(record3);
}
}
}
答案 1 :(得分:0)
据我所知,您似乎将每条记录的信息存储在单独的数组中,每个数组分别用于终端,离开,起点和目的地。这有点问题,因为由于某种原因没有确定信息属于同一记录的具体方法,所以信息按照记录在数组中排序:
示例:
ow_terminal[0] = 1
ow_depart[0] = "US"
ow_origin[0] = "UK"
ow_destination[0] = 10.00
如果是这种情况,则必须更改if语句逻辑。在这种情况下,我不认为LINQ可以提供帮助,但您可以尝试:
bool exists = false;
for(int i = 0; i < ow_terminal.Count; i++)
{
if(ddlterminal.SelectedValue.Contains(ow_terminal[i])
&& ddlDeparture.SelectedValue.Contains(ow_depart[i])
&& ddlOrigin.SelectedValue.Contains(ow_origin[i])
&& ddlDestination.SelectedValue.Contains(ow_destination[i]))
{
exists = true;
break;
}
}
if(exists) lblMessage.Text = "Record exist";
else lblMessage.Text = "No record exist";
代码检查相同位置的每个数组的值是否相等。同样,只有在每个记录上排列所有数组上的项目时,这才有效。此外,您可能想要更改相等性检查,因为Contains
可能不是最适合您的目的(例如,“US”在
但是,管理四个阵列并确保它们始终正确排序是非常脆弱的。你想要做的是只创建一个与你的记录对应的对象数组(或列表)以及相应的属性:
public class FlightRecord
{
public int Terminal {get;set;}
public string From {get;set;}
public string To {get;set;}
public decimal Time {get;set;} // How are you storing time?
}
从您的数据库记录中创建一个FlightRecords数组,然后您可以通过一些小的更改来执行您想要的逻辑:
if (ow_flightrecords.Any(s => ddlterminal.SelectedValue.Contains(s.Terminal.ToString()
&& ddlDeparture.SelectedValue.Contains(s.Time.ToString())
&& ddlOrigin.SelectedValue.Contains(s.From)
&& ddlDestination.SelectedValue.Contains(s.To)))
{
lblMessage.Text = "Record exist";
}
else
{
lblMessage.Text = "No record exist"
}
这个更强大,您可以轻松使用LINQ。再次,改变你进行比较的方式,因为Contains
可能不如你想象的那样适合你的目的。
正如Synchro所说,在对数据库进行实际INSERT之前,通过存储过程直接检查数据库可能是个好主意。这样会快得多。