我有一个提取一些记录的 linq 查询。它可以是 1 条记录、2 条记录或 30 条记录。我有两个文本框,如果只有 1 条记录或只有 2 条记录,我希望记录进入,然后如果它超过它,它会将它放在下拉列表中。这就是我到目前为止所拥有的。
var getContacts = (from r in gServiceContext.CreateQuery("account")
join c in gServiceContext.CreateQuery("contact") on ((EntityReference) r["accountid"]).Id
equals c["accountid"]
where r["accountid"].Equals(ddlCustomer.SelectedValue)
select new
{
FirstName = !c.Contains("firstname") ? string.Empty : c["firstname"],
LastName = !c.Contains("lastname") ? string.Empty : c["lastname"],
});
foreach (var contact in getContacts)
{
if (getContacts.ToList().Count() == 1)
{
txtContact1Name.Text = contact.FirstName + " " + contact.LastName;
}
else if (getContacts.ToList().Count() == 2)
{
txtContact2Name.Text = contact.FirstName + " " + contact.LastName;
}
else if (getContacts.ToList().Count() > 2)
{
ddlMultipleContacts.DataSource = getContacts;
ddlMultipleContacts.DataTextField = "LastName";
ddlMultipleContacts.DataValueField = "LastName";
ddlMultipleContacts.DataBind();
}
}
但如果有两条记录,它会将相同的记录放在文本框 1 和文本框 2 中。难道我做错了什么?
谢谢!