日期:2014-05-16  浏览次数:20846 次

.net 字符串较长 保存问题 效率问题
string  Str=”77181,77180,77179,77178,77177,77176,77175,77174,77173,77172,77171,77170,77169,77168,77167,77166,77165,77164,77163,77162,77161,77160,77159,77158,77157,77156,77155,77154,77153,77152,77151,77150,77149,77148,77147,77146,77145,77144,77143,77142,77141,77140,77139,77138,77137,77136,77135,77134,77133,77132,77131,77130,77129,77128,77127,77126,77125,77124,77123,77122,77121,77120,77119,77118,77117,77116,77115,77114,77113,77112,77111,77110,77109,77108,77107,77106,77105,77104,77103,77102,77101,77100,77099,77098“;


这只是当中的一部分
这是前端post过来的字符串,最后通过这些ID查询数据拼返回一个list列表
求优化.

  public List<House> ListHouse(string id)
        {
            using (VolcanoEntities db = new VolcanoEntities())
            {
                List<House> result = new List<House>();
                //id = id.Substring(0, id.Length - 1);
                string[] idStr = id.Split(',');
                if (idStr.Length > 0)
                {
                    House Excelhouse = new House();
                    foreach (var item in idStr)
                    {
                        int it = int.Parse(item);
                        Excelhouse = db.House.Where(i => i.Id == it).FirstOrDefault();
                        result.Add(Excelhouse);
                    }
                }
                return result;
            }
        }

------解决方案--------------------
select * From tb Where @stridlist like '%,'+ cast(id as nvarchar(10))+',%'


 @stridlist=","+ "77181,77180,77179,77178,77177,77176,77175,77174,77173,77172,77171,77170,77169,77168,77167,77166,77165,77164,77163,77162,77161,77160,77159,77158,77157,77156,77155,77154,77153,77152,77151,77150,77149,77148,77147,77146,77145,77144,77143,77142,77141,77140,77139,77138,77137,77136,77135,77134,77133,7