2010-05-05 101 views
1

我有一个表,看起来像这样:VB.NET LINQ结果集操作

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列值。

+0

您想要显示dateValue,其中Description =“FRRCD”。 。 。 5次?重复?这听起来不对。 – 2010-05-05 17:20:40

+0

不,我会有lbl1Class.Text其中Description = FRRSD,lbl2Class.Text其中Description = SORSD等 – davemackey 2010-05-05 17:30:19

回答

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"]) 

您可能需要做一些检查它们的存在了。另外,如果说明在结果中不唯一,您将得到一个异常

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 %>