0

我有一个提取一些记录的 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 中。难道我做错了什么?

谢谢!

4

2 回答 2

1

但如果有两条记录,它会将相同的记录放在文本框 1 和文本框 2 中。难道我做错了什么?

是的。看看你的代码:

if (getContacts.ToList().Count() == 2)

您在每次迭代时都调用它 - 您没有使用您已经使用过多少联系人的计数。我怀疑你想要:

// Let's only materialize the results *once* instead of once per iteration...
var contacts = getContacts().ToList();
switch (contacts.Count)
{
    case 0: // What do you want to do here?
        break;
    case 1:
        txtContact1Name.Text = FormatName(contacts[0]);
        break;
    case 2:
        txtContact1Name.Text = FormatName(contacts[0]);
        txtContact2Name.Text = FormatName(contacts[1]);
        break;
    default:
        ddlMultipleContacts.DataSource = contacts;
        ddlMultipleContacts.DataTextField = "LastName";
        ddlMultipleContacts.DataValueField = "LastName";
        ddlMultipleContacts.DataBind();
        break;
}
于 2012-05-30T18:43:54.737 回答
0
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"],
                                      }).ToList();


        if (getContacts.Count > 2)
        {
            ddlMultipleContacts.DataSource = getContacts;
            ddlMultipleContacts.DataTextField = "LastName";
            ddlMultipleContacts.DataValueField = "LastName";
            ddlMultipleContacts.DataBind();
        }
        else if (getContacts.Count == 1)
        {
            txtContact1Name.Text = contact.FirstName + " " + contact.LastName;
        }
        else if (getContacts.Count == 2)
        {
            txtContact1Name.Text = contact[0].FirstName + " " + contact[0].LastName;
            txtContact2Name.Text = contact[1].FirstName + " " + contact[1].LastName;
        }
于 2012-05-30T18:50:58.327 回答