循环并将结果分配给文本框

时间:2012-05-30 18:39:09

标签: c#

我有一个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中放置相同的记录。我做错了吗?

谢谢!

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
        }