简体   繁体   English

将由LINQ语句生成的字符串解析为方法

[英]Parsing a string made from a LINQ statement to a method

I want to pass only e.Trip of my string.Format to the method referenced in tripChoose . 我想通过只e.Trip我的string.Format中引用的方法tripChoose As of now, it tried to pass a string not formatted for the method. 截至目前,它试图传递一个未为该方法格式化的字符串。 If I could just pass the Trip part of the string, it would work. 如果我可以通过字符串的Trip部分,它会工作。 Any suggestions? 有什么建议么?

private void LoadExpenseListSums()
{
    expenseTotalSelect.Items.Clear();
    var sortedList=
        from e in roster
        group e by e.Trip into expenseCollect
        select new { Trip = expenseCollect.Key, SumAmount = expenseCollect.Sum(e => e.Amount) };
    foreach (var e in sortedList)
        tripChoose.Items.Add(string.Format("{0} | ${1}", e.Trip, e.SumAmount));
}

It looks like your expenseTotalSelect is an ASP.NET Select control and what you're doing ends up adding an element in the result HTML identified only by text such as: 看起来您的expenseTotalSelect是一个ASP.NET Select控件,您正在做的事情最终会在结果HTML中添加一个元素,该元素仅由文本标识,例如:

<select id='expenseTotalSelect'>
    <option>Hawaii | $5000</option>
    <option>London | $4000</option>
    ...
</select>

In your expenseTotalSelect.Items.Add call you would probably be better off doing the following: 在您的expenseTotalSelect.Items.Add调用中,您可能最好不要执行以下操作:

expenseTotalSelect.Items.Add(
       new ListItem(string.Format("{0} | ${1}", e.Trip, e.SumAmount),e.Trip));

Now you'll get the following when you render to HTML: 现在,当您渲染为HTML时,您将获得以下内容:

<select id='expenseTotalSelect'>
    <option value='Hawaii'>Hawaii | $5000</option>
    <option value='London'>London | $4000</option>
    ...
</select>

And it will be easier to just reference tripChoose.SelectedItem.Value which will only contain the e.Trip value (Hawaii or London) for this example 并且更容易引用tripChoose.SelectedItem.Value ,它仅包含此示例的e.Trip值(夏威夷或伦敦)

Right, now we actually know what we're dealing with, I suspect you want something like this: 是的,现在我们实际上知道我们正在处理什么,我怀疑你想要这样的东西:

private void LoadExpenseListSums()
{
    expenseTotalSelect.Items.Clear();
    var dateSorted =
        from e in roster
        group e by e.Trip into tripGroup
        select new { Trip = tripGroup.Key,
                     Text = string.Format("{0} | ${1}",
                                          tripGroup.Key,
                                          tripGroup.Sum(e => e.Amount) };
    tripChoose.DataSource = dateSorted;
    tripChoose.DisplayMember = "Text";
    tripChoose.ValueMember = "Trip";
}

This makes a few assumptions: ase, - roster is an in-memory collection: if it's not, you'll probably need an AsEnumerable call before the formatting, to make that happen locally - Anonymous types work with data binding; 这做了一些假设:ase, - roster是一个内存中的集合:如果不是,你可能需要在格式化之前调用AsEnumerable ,以便在本地发生 - 匿名类型使用数据绑定; I'm not sure whether that's the case, but I'd hope it is 我不确定是否是这种情况,但我希望是这样的

You then use the SelectedValue member later on to find the trip ID. 然后,您稍后使用SelectedValue成员来查找行程ID。

认为您应该使用注释中建议的对象,但假设expenseTotalSelectList<string>并且tripChoose绑定到它,您可以这样做:

(string)tripChoose.SelectedItem.Split('|')[0].Replace(" ", string.Empty)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM