LINQ关联表查询语法和.NET扩展方法和JSON.NET时间格式化代码段

  

  

 public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string customerid = context.Request["customerid"];
        using (CrcShDbDataContext crc = new CrcShDbDataContext())
        {
            var query = from a in crc.dbo_pre_database_phone
                        join b in crc.dbo_pre_database_extinfo on a.phoneID equals b.phoneID
                        where a.客户ID == Int32.Parse(customerid)
                        orderby a.phoneID
                        select new
                        {
                            a.客户ID,
                            a.phoneID,
                            a.ph,
                            b.extinfoID,
                            b.Mail,
                            b.MSN,
                            b.QQ,
                            b.创建时间,
                            b.官网,
                            b.决策类型,
                            b.联系地址,
                            b.联系公司名,
                            b.联系人,
                            b.其他联系方式,
                            b.性别,
                            b.职务,
                            来源 = crc.dbo_pre_database_src.Where(n => n.extinfoID == b.extinfoID).ExtensionQueryable()//关联子
                        };

            var dataobj = new { rows = query, total = query.Count() };
            
            //JSON.NET格式化时间
            var timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
            
            var jsonstr = Newtonsoft.Json.JsonConvert.SerializeObject(dataobj, Newtonsoft.Json.Formatting.Indented, timeConverter);
            context.Response.Write(jsonstr);


        }
    }

ExtensionQueryable扩展方法
public static class ExtensionIQueryable
{

    public static string ExtensionQueryable(this IQueryable<dbo_pre_database_src> queryable)
    {
        StringBuilder sb = new StringBuilder();
        foreach (var s in queryable)
        {
            sb.Append(s.source);
            sb.Append("&");
        }
        if (sb.Length > 0)
        {
            sb.Remove(sb.Length - 1, 1);
        }

        return sb.ToString();
    }




}

 






posted @ 2014-04-22 11:23  不要用我的二来伤害我  阅读(299)  评论(0编辑  收藏  举报