如何以修改形式编写以下代码 [英] how to write the following code in modified form

查看:64
本文介绍了如何以修改形式编写以下代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

plz帮助我减少代码行:


plz help me to reduce the lines of code:


IQueryable<string> txt = (from mar in db.Markets
                                         where mar.MarketID == Int32.Parse(textBox1.Text)
                                         select mar.MarketName);
               textBox2.Text = txt.Single();

               IQueryable<string> startDate = (from mar in db.Markets
                                               where mar.MarketID == Int32.Parse(textBox1.Text)
                                               select (mar.StartDate).ToString());
               dateTimePicker6.Text= startDate.Single();
               IQueryable<string> endDate = (from mar in db.Markets
                                             where mar.MarketID == Int32.Parse(textBox1.Text)
                                             select (mar.EndDate).ToString());
               dateTimePicker7.Text = (endDate.Single());
               IQueryable<string> lUpdate = (from mar in db.Markets
                                             where mar.MarketID == Int32.Parse(textBox1.Text)
                                             select (mar.LastUpdatedDate).ToString());
               dateTimePicker8.Text = (lUpdate.Single());

               IQueryable<string> version = (from mar in db.Markets
                                             where mar.MarketID == Int32.Parse(textBox1.Text)
                                             select mar.Version);
               textBox8.Text = version.Single();
               IQueryable<string> lastUpdateBy = (from mar in db.Markets
                                                  where mar.MarketID == Int32.Parse(textBox1.Text)
                                                  select mar.LastUpdatedBy);
               textBox9.Text = lastUpdateBy.Single();


IQueryable<string> cName1 = (from cur in db.Currencies
                                            where cur.CurrencyID == Int32.Parse(cId.Text)
                                            select cur.CurrencyName);
                cName.Text = cName1.Single();
                IQueryable<string> Descrp = (from cur in db.Currencies
                                             where cur.CurrencyID == Int32.Parse(cId.Text)
                                             select cur.Description);
                Des.Text = Descr.Single();
                IQueryable<string> Update = (from cur in db.Currencies
                                                where cur.CurrencyID == Int32.Parse(cId.Text)
                                                select cur.LastUpdatedBy);
                Lupdated.Text = Update.Single();

                IQueryable<string> curUpdate = (from cur in db.Currencies
                                                where cur.CurrencyID == Int32.Parse(cId.Text)
                                                select (cur.LastUpdatedDate).ToString());
                curUpdated.Text = curUpdate.Single();


IQueryable<string> commUpdatedBy = (from com in db.Commodities
                                                    where com.CommodityID == Int32.Parse(textBox4.Text)
                                                    select com.LastUpdatedBy);
                textBox17.Text = commUpdatedBy .Single();
                IQueryable<string> comVersion = (from com in db.Commodities
                                                    where com.CommodityID == Int32.Parse(textBox4.Text)
                                                    select com.Version);
                textBox15.Text = comVersion.Single();

                IQueryable<string> sdate = (from com in db.Commodities
                                            where com.CommodityID == Int32.Parse(cId.Text)
                                            select (com.StartDate).ToString());
                dateTimePicker3.Text = sdate.Single();
                IQueryable<string> edate = (from com in db.Commodities
                                            where com.CommodityID == Int32.Parse(cId.Text)
                                            select (com.EndDate).ToString());

推荐答案

我建​​议您下载
I suggest you download LINQPad, play around with it, but don''t forget to consult MSDN.

But here is the first one:
textBox2.Text = db.Markets.Single(x => x.MarketID == Int32.Parse(textBox1.Text)).MarketName;


其他所有的人都喜欢这个.我知道,这不是LINQ,它是流利的api,但是在某些情况下,它是较短的api.就像这里.

请注意,如果结果集不完全包含一个元素,则Single将引发异常. FirstOrDefault有时会更好.


All others are like this one. I know, this is not LINQ it is fluent api, but there are cases, where it is the shorter one. Like here.

Be aware, that Single will throw an exception if the result set does not contain exactly one element. FirstOrDefault is sometimes better.


这篇关于如何以修改形式编写以下代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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