存储数据类型的转化总结

Object类型集合对象或者Linq集合对象转化成Json数据

 public JsonResult List()
        {
            //List<T_MaterialType> list = db.T_MaterialType.ToList();

            //List<Object> result = new List<object>();

            //foreach (T_MaterialType a in list)
            //{
            //    result.Add(new
            //    {
            //        MaterialTypeID = a.MaterialTypeID,
            //        Factory = a.Factory,
            //        Unit = a.Unit,
            //        Model = a.Model,
            //        TypeName = a.TypeName
            //    });
            //}

            //return Json(list);


     var q = from type in db.T_MaterialType
                    select new 
                    {
                        Material = type.MaterialTypeID,
                        Factory = type.Factory,
                        Unit = type.Unit,
                        Model = type.Model,
                        TypeName = type.TypeName
                    };
            return Json(q);

        }






从集合中取出单个对象
关于linq to sql语句返回值得问题
2011-8-1 16:08
提问者: 发了的风仔 | 浏览次数:223次

例如
public IQueryable<int> GetUserID(string userName)
        {
            return from user in db.users
                   where user.userName == userName
                   select user.userID;
        }
请问怎么才能把 IQueryable<int>转换成int呢?

问题补充:

在Controller中转换:
userID=Convert.ToInt16(userRepository.GetUserID(User.Identity.Name))
报错:无法将类型为“System.Data.Linq.DataQuery`1[System.Int32]”的对象强制转换为类型“System.IConvertible”。

或者有没有别的方法根据userName提取出userID???
我用的是.net

我来帮他解答
2011-8-1 17:02
满意回答

oh!mygod! 集合怎么能转化成单个对象呢!只能从集合中取一个变成单个的对象!
---如下类似而已!保证一定存在用First,不一定用FirstOrDefault()
(from x in db.users where x.userName == userName select x).First().userID
或是如下方式

仅此而已! 当然第二种好一些!

1
| 评论
向TA求助

回答者: edwin_tech 来自团队 烈火编程 | 七级采纳率:57%

擅长领域: 数据库 C#/.NET JAVA相关 操作系统/系统故障 办公软件

参加的活动: 暂时没有参加的活动

提问者对回答的评价:

谢谢啊,受教了~







泛型集合,DataTable
private DataTable toDataTable(List<T_MaterialInfo> q)
    {
        T_MaterialInfo mater;
        DataTable dt = new DataTable();
        dt.Columns.Add("MaterialInfoID");
        dt.Columns.Add("MaterialTypeID");
        dt.Columns.Add("Name");
        dt.Columns.Add("Number");
        dt.Columns.Add("Desc");

        dt.Columns.Add("InStoreUser");
        try
        {
            if (q.Count > 0)
            {
                for (int i = 0; i < q.Count; i++)
                {
                    mater = q[i];
                    DataRow dr = dt.NewRow();
                    dr["MaterialInfoID"] = mater.MaterialInfoID;
                    dr["MaterialTypeID"] = mater.MaterialTypeID;
                    dr["Name"] = mater.Name;
                    dr["Number"] = mater.Number;
                    dr["Desc"] = mater.Desc;
                    dr["InStoreUser"] = mater.InStoreUser;
                    dt.Rows.Add(dr);

                }
            }
        }
        catch (Exception e)
        {

            throw e;
        }
        return dt;
    }






数组,泛型集合,JSON数据
public JsonResult GetListMapoffset(List<string> id)
        {
            string[] list=new string[id.Count];
            //int i = 0;
            //foreach (string s in id)
            //{
            //    list[i] = s;
            //    i++;
            //}
            for (int i = 0; i < id.Count; i++)
            {
                list[i] = id[i];
            }
            return Json(list, JsonRequestBehavior.AllowGet);





数组,DataTable
 public JsonResult GetListMapOffset(List<string> lnglat)
        {
            JsonResult result = new JsonResult();
            //DataTable dt = new DataTable();
            //dt.Columns.Add("lat", typeof(int));
            //dt.Columns.Add("lng", typeof(int));
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            List<object> list = new List<object>();
            try
            {
                foreach (var temp in lnglat)
                {
                    string[] t = temp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (t.Length != 2)
                        continue;

                    int lng = Int32.Parse(t[0]);
                    int lat = Int32.Parse(t[1]);
                    //dt.Rows.Add(lat, lng);

                    var q = from m in db.T_MapOffset.Where(p => p.lng == lng && p.lat == lat) select new { m.lat, m.lng, m.latoffset, m.lngoffset };
                    list.AddRange(q);
                }


                return Json(list);
            }
            catch
            {
                result.Data = null;
            }

            return result;

        }







循环集合中的单个对象调用某个方法
  //public void str()
        //{
        //    var v = from e in db.T_VehicleInfo
        //            select e.VehicleID;
        //            foreach(var id in v)
        //            {
        //                string str = GetVehiclePhoto(id);
        //                Response.Write(str+"");
        //            }
            
        //}








字符串,DataTable
public JsonResult GetListMapOffset(List<string> lnglat)
        {
            JsonResult result = new JsonResult();
            DataTable dt = new DataTable();
            dt.Columns.Add("lat", typeof(int));
            dt.Columns.Add("lng", typeof(int));
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            List<object> list = new List<object>();
            try
            {
                foreach (var temp in lnglat)
                {
                    string[] t = temp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (t.Length != 2)
                        continue;

                    int lng = Int32.Parse(t[0]);
                    int lat = Int32.Parse(t[1]);
                    dt.Rows.Add(lat, lng);

                    var q = from m in db.T_MapOffset.Where(p => p.lng == lng && p.lat == lat) select new { m.lat, m.lng, m.latoffset, m.lngoffset };
                    list.AddRange(q);
                }


                return Json(list);
            }
            catch
            {
                result.Data = null;
            }

            return result;

        }






字符串,数组
 public JsonResult GetListForPlan(string bsids)
        {
            string[] list = bsids.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
            if(list.Length<=0)
            {
                return null;
            }
            int[] ids=new int[list.Length];
            int i = 0;
            foreach (string s in list)
            {
                ids[i] = int.Parse(s);
                i++;
            }

            var q = from e in db.T_BaseStation
                    join a in ids on e.BSID equals a
                    select new
                    {
                        BSID = e.BSID,
                        Name = e.Name,
                        Code = e.Code,
                        RoomID = e.RoomID,
                        Building = e.Building,
                        Type = e.Type,
                        Address = e.Address,
                        LocationInBuilding = e.LocationInBuilding,
                        Longitude = e.Longitude,
                        Latitude = e.Latitude,
                        LonLatUpdateDate = e.LonLatUpdateDate,
                        VIP = e.VIP,
                        InspectorArrivalTime = e.InspectorArrivalTime,
                        PowerGenerationArrivalTime = e.PowerGenerationArrivalTime,
                        LastInspectTime = e.LastInspectTime,
                        BestInspectCircle = e.BestInspectCircle,
                        AddUser = e.AddUser,
                        AddTime = e.AddTime,
                        MonitoringInstalled = e.MonitoringInstalled,
                        ResponsibleUser = e.ResponsibleUser,
                        DepartmentID = e.DepartmentID,
                        Network = e.Network
                    };


            return Json(q, JsonRequestBehavior.AllowGet);
        }

 

你可能感兴趣的:(数据类型)