Linq equivalent of SQL LEFT function?

前端 未结 3 1538
离开以前
离开以前 2021-01-19 10:48

We have a database with some fields that are varchar(max) which could contain lots of text however I have a situation where I only want to select the first for example 300 c

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-19 11:15

    I found something like this worked for me:

    return from locationPart in db.locations
           select new LocationPart
           {                       
             Description = locationPart.description,
             Text = locationPart.text.Substring(0,300)                       
           };
    

    Not ideal because I have to use "select new" to return a a different object, but it seems to work.

提交回复
热议问题