简体   繁体   中英

LEFT AND RIGHT function lambda expression c#

How can I from using LEFT AND RIGHT function lambda expression in c#? for example:

"SELECT [Code]   FROM [City]  WHERE LEFT ([Code] ,3)=" + Digit + " LEFT AND RIGHT function in ([Code],6)<>111111 order by Code"

Can you help me to convert the above code to lambda expression in c#

Base on your example, assuming you tried to format string with lambda. Please let me know if I misunderstand what you try to achieve.

Func<int, string> myFunc = d => $"SELECT [Code]   FROM [City]  WHERE LEFT ([Code] ,3)={d} LEFT AND RIGHT function in ([Code],6)<>111111 order by Code";
public class CityCustomRepository:GenericCrudRepository<City>
    {
        private ShopEntities _db;
        public CityCustomRepository(ShopEntities db) : base(db)
        {
            _db = db;
        }

        public List<City> GetAllCityCustom(int StartDigit)
        {
            return _db.City.Where(c=> DbFunctions.Left(c.Id.ToString(), 2) == StartDigit.ToString() && DbFunctions.Right(c.Id.ToString(), 5) <> "00000").ToList();
           
        }
    }
  1. To select the first city, I select the province

2- Province code 30

3- The province code is taken

4- The city code is 7 digits

2 digits of city code are separated from the left and compared with 2 digits of provincial code

And after that, the 5 digits to the right of the city code will be separated, this time it must be opposite to 00,000

I found my answer

public List<City> GetAllCity(object StartDigit)
        {
            try
            {
                return _db.City.Where(c => c.Id.ToString().StartsWith(StartDigit.ToString()) && !c.Id.ToString().EndsWith("00000")).ToList();
            }
            catch
            {
                return null;
            }
            
        }

Just consider the type of data type as an object

GetAllCityCustom(int StartDigit)

And use the following command

c => c.Id.ToString().StartsWith(StartDigit.ToString()) &&.c.Id.ToString().EndsWith("00000")

Thanks for the tips dear friends

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM