如何解决无法将最大值搜索到数组中
我的问题是我无法使用linq搜索数组以查找对象属性并将其设置为ID。
我需要一种在数组中搜索其他model.idCliente
并将该值设置为“ nextid + 1”的方法,以便将其用作id和下一个数组索引。
由于数组为空,因此程序会正确添加新对象,但是在else情况下输入时,我会收到“ a as null”的表示。
这是我的代码(如果linq行说“ a”为空,则在else上获得异常):
//Arrays
ClienteModel[] MemoryClienti = new ClienteModel[19];
OrdineModel[] MemoryOrdini = new OrdineModel[19];
//Aggiungi
public bool CreateCliente(ClienteModel model)
{
if (MemoryClienti[0] == null)
{
int defaultID = 0;
int defaultIndex = 0;
model.IDCliente = defaultID;
MemoryClienti[defaultIndex] = model;
}
else if (MemoryClienti[0]!=null)
{
var maxID = MemoryClienti.Max(a => a.IDCliente);
model.IDCliente = maxID++;
MemoryClienti[maxID++] = model;
}
return true;
}
这是单击形式的代码:
//Aggiungi Cliente
private void aggiungiClienteButton_Click(object sender,EventArgs e)
{
clienteModel.Cognome = cognomeTextBox.Text;
clienteModel.Nome = nomeTextBox.Text;
clienteModel.Indirizzo = indirizzoTextbox.Text;
dbMemoryManager.CreateCliente(clienteModel);
MessageBox.Show("Cliente aggiunto correttamente.");
cognomeTextBox.Text = String.Empty;
nomeTextBox.Text = String.Empty;
indirizzoTextbox.Text = String.Empty;
}
这是ClienteModel
类:
public class ClienteModel
{
public int IDCliente { get; set; }
public string Cognome { get; set; }
public string Nome { get; set; }
public string Indirizzo { get; set; }
}
解决方法
以下代码无法实现您想要的工作吗?
<div id="container">
<div class="shelf">
<div class="labelbox">Name: </div> <div class="valuebox">Barry Carter</div>
</div>
<div class="shelf">
<div class="labelbox">DOB:</div><div class="valuebox">10/12/1980</div>
</div>
<div class="shelf">
<div class="labelbox">
Description:
</div>
<div class="valuebox">
This content goes on and on and will force the height to expand. And the label box to the left will
"move" with it. There need not be much of a relation other than that their parent div/flex-container is
getting taller as well.
</div>
</div>
<div class="shelf">
<div class="labelbox">Group:</div><div class="valuebox">Advanced</div>
</div>
<div class="shelf">
<div class="labelbox">End Date:</div><div class="valuebox">2020-09-20</div>
</div>
</div>
如果您也要进行删除,最好使用列表,就像其他人也建议的那样。
,如Flydog57所述,在这种情况下,您应该使用列表而不是数组-每当您使用.Max()时,在这种情况下,您正在查询具有空插槽的数组,这将返回错误,例如类没有可区分的默认值。
如果要保留当前的逻辑(包含数组和填充物),请尝试使用Struct而不是Class。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。