如何在LINQ中使用Select语句 [英] How to use Select Statement in LINQ

查看:92
本文介绍了如何在LINQ中使用Select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我有这个简单的会话表,它有一个列Enddate,StartDate



我刚用Linq编写这个简单的代码来选择StartDate页面加载

  var  acc = sdc.Sessions 
.Where(x = > x.StartDate == DateTime.Now.ToString())
.Select(x = > new {x.StartDate});
Label1.Text = acc.ToString();

// 预览而不是向我显示它将显示给我的日期
// SELECT [t0]。[StartDate] FROM [dbo]。[Session] AS [t0 ] WHERE [t0]。[StartDate] = @ p0

// 而不是StartDate列上的实际日期...有人应该帮助我谢谢

解决方案

嗨试试这样,,





其中子句返回多个结果,超过其结果属于 IEnumerable< t> < / t>

你应该使用 First,FirstOrDefault,Single,SingleOrDefault或Index。来获取用于阅读该属性的集合中的单项值。



  var  acc = sdc.Sessions.FirstOrDefault(x = >  x.StartDate == DateTime.Now.ToString()); 
if (acc!= null
Label1.Text = acc.StartDate的ToString();


good day all, i have this simple table of session, it has a column Enddate, StartDate

I just wrote this simple code using Linq to select the StartDate on page load

  var acc = sdc.Sessions
                .Where(x=> x.StartDate == DateTime.Now.ToString())
                .Select(x => new { x.StartDate });
            Label1.Text = acc.ToString();

// on preview instead of showing me the date it will show me this
//SELECT [t0].[StartDate] FROM [dbo].[Session] AS [t0] WHERE [t0].[StartDate] = @p0 

//Instead the actual date on the StartDate column... Someone should please help me out thanks

解决方案

Hi Try like this,,


Where clause returns more than one results, more over its is of type IEnumerable<t> </t>.
You should use First,FirstOrDefault, Single ,SingleOrDefault , or Index . to get single item value from the collection for reading the property.

var acc = sdc.Sessions.FirstOrDefault(x => x.StartDate == DateTime.Now.ToString());
           if( acc != null)
           Label1.Text = acc.StartDate.ToString();


这篇关于如何在LINQ中使用Select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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