C# 如何在repeater中绑定前拆分字符串

C# 如何在repeater中绑定前拆分字符串,c#,asp.net,linq,C#,Asp.net,Linq,我有这样一个Linq查询: var result = from c in db.Class join s in db.Students on c.Cls_Id equals s.Cls_Id select new { s.Stud_Id, s.FirstName, c.C

我有这样一个Linq查询:

var result = from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new
                 {
                     s.Stud_Id,
                     s.FirstName,
                     c.Cls_Id,
                     c.Room,
                     c.Notification
                 };
repeater.DataSource = result.ToList();
repeater.DataBind();
但在通知字段中有这样的内容:C编程类/NTFF的这个房间。如果在标签
Text=''
中绑定,它将显示:这个C编程类房间/NTFF

我想将此字符串拆分为两个字符串,如下所示:

var result = from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new
                 {
                     s.Stud_Id,
                     s.FirstName,
                     c.Cls_Id,
                     c.Room,
                     c.Notification
                 };
repeater.DataSource = result.ToList();
repeater.DataBind();
str1=C编程类的这个房间

str2=NTFF

在将str1绑定到标签1和str2绑定到标签2之前。我怎样才能做到这一点?

您可以使用函数来获取str1,如下所示:-

 var result = from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new
                 {
                     s.Stud_Id,
                     s.FirstName,
                     c.Cls_Id,
                     c.Room,
                     str1 = c.Notification.Split('/').FirstOrDefault()
                 };
<asp:Lable Text='<%#DataBinder.Eval(Container.DataItem, "str1")%>'><asp:Label/>
public class Students
{
   public int Stud_Id { get; set; }
   public string FirstName{ get; set; }
   public int Cls_Id{ get; set; }
   public string Room{ get; set; }
   public string Notification{ get; set; }
   public string str1{ get; set; }
   public string str2{ get; set; }
}
List<Students> students = (from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new Students
                 {
                     Stud_Id = s.Stud_Id,
                     FirstName = s.FirstName,
                     Cls_Id = c.Cls_Id,
                     Room = c.Room,
                     Notification= c.Notification
                 }).ToList();
foreach (Student student in students)
{
    string[] Notifications = student.Notification.Split('/');
    student.str1 = Notifications.FirstOrDefault();
    student.str2 = Notifications.ElementAtOrDefault(1);
 }
然后,您可以将其绑定到标签,如下所示:-

 var result = from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new
                 {
                     s.Stud_Id,
                     s.FirstName,
                     c.Cls_Id,
                     c.Room,
                     str1 = c.Notification.Split('/').FirstOrDefault()
                 };
<asp:Lable Text='<%#DataBinder.Eval(Container.DataItem, "str1")%>'><asp:Label/>
public class Students
{
   public int Stud_Id { get; set; }
   public string FirstName{ get; set; }
   public int Cls_Id{ get; set; }
   public string Room{ get; set; }
   public string Notification{ get; set; }
   public string str1{ get; set; }
   public string str2{ get; set; }
}
List<Students> students = (from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new Students
                 {
                     Stud_Id = s.Stud_Id,
                     FirstName = s.FirstName,
                     Cls_Id = c.Cls_Id,
                     Room = c.Room,
                     Notification= c.Notification
                 }).ToList();
foreach (Student student in students)
{
    string[] Notifications = student.Notification.Split('/');
    student.str1 = Notifications.FirstOrDefault();
    student.str2 = Notifications.ElementAtOrDefault(1);
 }
然后,首先用如下查询填充自定义类:-

 var result = from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new
                 {
                     s.Stud_Id,
                     s.FirstName,
                     c.Cls_Id,
                     c.Room,
                     str1 = c.Notification.Split('/').FirstOrDefault()
                 };
<asp:Lable Text='<%#DataBinder.Eval(Container.DataItem, "str1")%>'><asp:Label/>
public class Students
{
   public int Stud_Id { get; set; }
   public string FirstName{ get; set; }
   public int Cls_Id{ get; set; }
   public string Room{ get; set; }
   public string Notification{ get; set; }
   public string str1{ get; set; }
   public string str2{ get; set; }
}
List<Students> students = (from c in db.Class
                 join s in db.Students on c.Cls_Id equals s.Cls_Id
                 select new Students
                 {
                     Stud_Id = s.Stud_Id,
                     FirstName = s.FirstName,
                     Cls_Id = c.Cls_Id,
                     Room = c.Room,
                     Notification= c.Notification
                 }).ToList();
foreach (Student student in students)
{
    string[] Notifications = student.Notification.Split('/');
    student.str1 = Notifications.FirstOrDefault();
    student.str2 = Notifications.ElementAtOrDefault(1);
 }
在此之后,只需使用参数
str1
str2
绑定标签即可使用
string.Replace()
,如下所示:

<%# ((string)DataBinder.Eval(Container.DataItem, "Notification")).Replace("/NTFF", string.Empty) %>
然后在前端使用
FirstNotification
SecondNotification
属性。
注意:当没有“/”字符时,上面的代码将抛出索引越界异常。

您可以这样使用:首先创建一个DTO来存储结果实体,其中包含所有字段和一个额外字段来存储通知列表

public class Result
{
    public int Stud_Id { get; set; }
    ...
    ...
    public string Notification { get; set; }
    public string[] Notifications { get; set; }
}

List<Result> result = from c in db.Class
    join s in db.Students on c.Cls_Id equals s.Cls_Id
    select new Result
    {
        Stud_Id = s.Stud_Id,
        ...
        ...
        Notification = c.Notification
    }).ToList();

result.ForEach(r =>
    {
        r.Notifications = r.Notification.Split('/');
    });

现在,您可以使用要在标签中绑定的任何方法。

但它会显示如下错误消息:LINQ to Entities无法识别“System.String[]Split(Char[])方法,并且该方法无法转换为存储表达式。@DucThoPham-Ohh您没有提到它正在使用实体框架。在这种情况下,您可以创建一个自定义类,并在收到查询中的数据后填充该类,并确保to必须调用
.ToList()
,才能在内存中显示结果。您能帮我演示一下吗?@DucThoPham-如果这对您有帮助,您可以接受它作为答案:)抱歉,但我想将其插入2个字符串。如何获取所有2个字符串查看我的更新答案,并让我知道它是否适用于您。对不起,如何使用语法绑定?始终尝试自己做一些事情。检查Text=''是否可以完成您的任务。@DucThoPham-如果此解决方案解决了您的查询,请标记为“答案”,以便其他人可以从中受益。