如何添加var类型变量中存在的值 [英] how can i add values which is present in a var type variable

查看:261
本文介绍了如何添加var类型变量中存在的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var prices = from ob in obj.Wishlist_tables

在st.movi​​e_id上​​的obj.movi​​e_details中加入X等于X.Id

其中st.username == Session [ 用户]。ToString()

选择新的{X.price}

现在我有一些产品的价格,我想添加它们并希望显示在标签

解决方案

好的,

尝试将你的linq查询更改为此,

  var  prices = 来自 st   obj.Wishlist_tables 
join X obj.movi​​e_details st.movi​​e_id等于X. Id
其中 st.username ==会话[ user]。ToString()
选择 X.price;

然后计算..

int count = 0 ;
foreach var objdata1 in 价格)
{
count + = Convert.ToInt32(objdata1);
}



count = prices.Sum();



希望它可以帮到你。 。


基本上var是编译器在运行时设置的新关键字。所以它取决于你期望var是什么。如果您期望var作为列表,则可以使用用于列表的常规语法,并可以为其添加值。

-SG


这样做



int totalPrices = prices.Sum();

var prices= 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 }
now i have some prices of some products and i want to add them and want to show in a label

解决方案

OK,
try to change your linq query to this,

var prices= 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 X.price; 

And then count it.. 

int count=0; 
foreach(var objdata1 in prices) 
{ 
    count+=Convert.ToInt32(objdata1); 
}

or

count = prices.Sum();


Hope it helps you..


basically var is new keyword which compiler set at run time. so it depened wht you are expecting that var to be. If you are expecting var as list you can use the normal syntax used for list and can add value to that.
-SG


do this

int totalPrices = prices.Sum();


这篇关于如何添加var类型变量中存在的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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