Using “Match” in a Linq statement

后端 未结 4 1372
南旧
南旧 2020-12-14 22:45

I have a table that has two records (there will be many at runtime). The deviceId of the records are, “DEVICE1” and “DEVICE2”. I want to use a regular expressio

4条回答
  •  情歌与酒
    2020-12-14 23:24

    You should always remember that your LinqToEntities queries must be translated to SQL queries. Since SQL Server has no support for regular expressions, this can not work.

    As suggested in the comment by Paul Ruane, StartsWith will work. This can be translated by LinqToEntities into WHERE DeviceId LIKE 'DEVICE%'.

    If StartsWith isn't enough because you may need to look for strings in the middle of database columns, Contains will also work:

    var devices = from d in ctx.Devices
                  where d.DeviceId.Contains("DEVICE")
                  select d;
    

    This will result in the following: WHERE DeviceId LIKE '%DEVICE%'.

提交回复
热议问题