-1

我想知道每个循环。我所拥有的是每个项目的几列,我想循环遍历它并从名称为“英寸”的每一列中取出值。

现在到值不仅是数字而且是字母的部分,例如“1英寸”。

所以我有 3 件物品,分别是“1 英寸”、“5 英寸”和“10 英寸”。

所以我想取出“英寸”(我猜是解析)并将这三个加在一起,所以总和是 16。

如果对于 windows phone 本地数据库 c#

我有一个 ViewModel 和一个模型。模型中的列如下所示

    private string _itemSpring;

    [Column]
    public string ItemSpring
    {
        get { return _itemSpring; }
        set
        {
            if (_itemSpring != value)
            {
                NotifyPropertyChanging("ItemSpring");
                _itemSpring = value;
                NotifyPropertyChanged("ItemSpring");
            }
        }
    }

然后我通过带有连接字符串到数据库的视图模型查看它,

// LINQ to SQL data context for the local database.
    public ToDoDataContext toDoDB;

    // Class constructor, create the data context object.
    public ToDoViewModel(string toDoDBConnectionString)
    {
        toDoDB = new ToDoDataContext(toDoDBConnectionString);
    }
4

1 回答 1

1

如果你的3个物品是这样的,

string[] items = { "1 inch", "5 inch", "10 inch" };
int totalCount = 0;
foreach(var item in items)
{
   string[] substrings = item.split(' ');
   totalCount += int.Parse(substrings[0]);
}
string finalString = totalCount + " inch";

更新:我不明白您是在询问如何从数据库中检索数据或如何处理从数据库中获取的数据。

假设第二种情况,检查以下答案

从数据库中获取数据并将其转换为 List 或 ObservableCollection,然后您可以使用以下查询来获取您想要的内容。

foreach(var item in ItemsList)
totalCount += double.Parse(item.ItemSize.Split(' ')[0]);

或其他

foreach(var item in ItemsList)
totalCount += double.Parse(item.ItemSize.Replace(" inch", ""));
于 2012-09-18T04:16:38.583 回答