Using Linq to Sql in C#, is there any way I can automatically truncate too-long data?

前端 未结 3 1935
孤城傲影
孤城傲影 2021-01-14 20:00

So, I\'m importing data from one database into another. There are about 5,000 records (so nothing ridiculous, but not small enough to eyeball). Is there an easy way to autom

3条回答
  •  清歌不尽
    2021-01-14 20:50

    I would make an extension iterator method that you could throw inline with your query, doing something like this:

    public static IEnumerable Truncater(this IEnumerable s, int len)
    {
        foreach( var str in s )
        {
            if( str.Length > len )
            {
                string outStr = str.Substring(0, len);
                Console.WriteLine(String.Format("Truncated {0} to {1}", str, outStr));
                yield return outStr;
            }
            else
                yield return str;
        }
    }
    

提交回复
热议问题