如何将linq查询的值放入数组中 [英] how to put the values of linq query in to an array

查看:91
本文介绍了如何将linq查询的值放入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var numb = (from st in obj.Wishlist_tables
                join X in obj.movie_details on st.movie_id equals X.Id
                where st.username == Session["user"].ToString()
                select new { X.price }).ToArray();
    int x = numb.Count();
    int[] ary = new int[x];
    Int32[] data = new Int32[x];
    int sum = 0;
    for (int i = 0; i < x; i++ )
    {
        data[i] =Convert.ToInt32(numb[i]);

    }

    for (int j = 0; j < x; j++)
    {
        sum += data[j];
    }

    lbl_totalprice.Text = sum.ToString();
}



这是查询

1st我想通过linq从数据库中找出值,这是我喜欢的现在我想将这些值存储在数组中然后我想添加这些值的ol


this is the query
1st i want to find out the values from database by linq which is fond and i puted that values in a var type varible numb now i want to store these values in array and then i want to add ol of these valus

推荐答案

因为你知道如何使用Linq:怎么样:
Since you know how to use Linq: how about:
lbl_totalprice.Text = numb.Sum(amt => Convert.ToInt32(amt)).ToString();

请注意,代码假定每个条目都在'numb是一个整数:所以,如果numb中的任何项不是整数,它将导致System.FormatException错误。

Note that code assumes that every entry in 'numb is an integer: so, it will cause a System.FormatException error, if any item in numb is not an integer.


在一个字符串中将string []转换为int []代码使用LINQ



检查
Convert string[] to int[] in one string of code using LINQ

Check it


这篇关于如何将linq查询的值放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆