VB.NET LINQ结果集操作

时间:2010-05-05 17:15:32

标签: .net vb.net linq linq-to-sql

我有一个看起来像这样的表:

ID / Description / textValue / dateValue / timeValue / Type
1  / FRRSD       / NULL      / 2010-04-16 00:00:00.000 / NULL / classdates

现在我有一个LINQ命令只从这个表中提取类型为classdates的行:

 Dim dbGetRegisterDates As New dcConfigDataContext
 Dim getDates = (From p In dbGetRegisterDates.webConfigOptions _
                Where p.Type = "classdates" _
                Select p)

我现在想要在五个不同的标签中显示数据,如下所示:

lblClass1.Text = "Your class is from " & getDates.Description("FRRSD").dateValue & "to " & getDates.Description("FRRCD").dateValue

lblClass2.Text = "Your class is from " & getDates.Description("SORSD").dateValue & "to " & getDates.Description("SORCD").dateValue

基本上,我想根据描述列值拉一行,然后从同一行返回datevalue列值。

2 个答案:

答案 0 :(得分:1)

这对你有用:

lblClass1.Text = From c in getDates Where Description = "FRRCD"
         Select string.Format("Your class is from {0} to {0}", c.dateValue)

lblClass2.Text = From c in getDates Where Description = "SORCD"
         Select string.Format("Your class is from {0} to {0}", c.dateValue)

我怀疑你真正想要的是为你找到的每条记录创建一个注释。更像是:

Dim sb = New StringBuilder()
For each c in getDates
    sb.Append(string.Format("<p><b>{0}</b>:  Your class is from {1} to {1}.</p>" _
        , c.Description, c.dateValue)
Next
classNotes = sb.toString()

然后在您的页面中放置<% = classNotes %>

答案 1 :(得分:1)

我不太了解VB.NET语法,所以我会用C#编写它,但你可以创建一个描述词典到日期。

var res = (from p dbGetRegisterDates.webConfigOptions
           where p.Type == "classdates"
           select new 
            {
               p.Description,
               p.dateValue
            }).ToDictionary(x=>x.Description, x=>x.dateValue);

然后你可以做

lblClass1.Text = string.Format("Your class is from {0} to {1}", res["FRRSD"], res["FRRCD"])

您可能想要检查它们是否也存在。此外,如果结果中的描述不唯一,您将获得异常