我有一个简单的搜索,我想返回一个非常简短的描述部分。我想抓住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
});
答案 0 :(得分:2)
您可以使用Take
方法:
Desc = new string(i.Description.Take(200).ToArray());
答案 1 :(得分:2)
Desc = i.Description.SubString(0, Math.Min(i.Description.Length, 200))