【问题标题】:how to use substring in linq c#如何在linq c#中使用子字符串
【发布时间】:2013-07-01 12:59:51
【问题描述】:

您好,我需要帮助才能在我的 liq 代码中使用子字符串来获取一些保存在 DB 中的借出数据。我想在 v.Description 中使用

v.Description.substring(0,399) 一些东西

这是我的代码

public static string BindeWorkOutDetails(int Id, Label Header, 
                     Label Title, Label Description)
        {
            DataClassesDataContext con = 
                  new DataClassesDataContext(con);
            var q = (from v in con.WorkoutSettings
                     where v.Id == Id
                     select new { v.Header ,v.Title, v.Description, 
                        v.ImageUrl }).First();
            Header.Text = q.Header;
            Title.Text = q.Title;            
            Description.Text = q.Description;
            return q.ImageUrl;
        }

请不要让我知道如何在 Linq 中使用子字符串,请重新编码我的这段代码

谢谢

【问题讨论】:

    标签: c# asp.net sql-server linq c#-4.0


    【解决方案1】:

    如果Description 是一个字符串,您可以执行以下操作:

    v.Description.Substring(0, 399);
    

    【讨论】:

    • 您也可以在将结果分配给Description.Text 时在 LINQ 查询之外执行此操作:Description.Text = q.Description.Substring(0, 399);
    • 但前提是 Description 总是长于或等于 399 个字符。如果您尝试使用少于 399 个字符的字符串进行此操作,您将获得 ArgumentOutOfRangeException。解决此问题的一种解决方案:v.Description.PadRight(399).Substring(0, 399).Trim()
    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2017-10-10
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多