返回简要说明

时间:2013-05-06 15:26:18

标签: c# asp.net linq entity-framework webforms

我有一个简单的搜索,我想返回一个非常简短的描述部分。我想抓住i.Description的前200个字符。这样做的最佳方式是什么?

  (
                      i.ItemName.Contains(keyword) ||
                      i.Email.Contains(keyword) ||
                      i.Description.Contains(keyword) ||
                      i.ItemID.Equals(searchID)
                      )
                         select new
                         {

                             Name = i.ItemName,
                             Phone = i.Phone,
                             Type = i.Type,
                             Email = i.Email,
                             ID = i.ItemID,
                             Desc = i.Description
                             // I was hoping i could do with a .Length or something    

                         });

2 个答案:

答案 0 :(得分:2)

您可以使用Take方法:

Desc = new string(i.Description.Take(200).ToArray());

答案 1 :(得分:2)

Desc = i.Description.SubString(0, Math.Min(i.Description.Length, 200))