首页 > 解决方案 > 使用 linq 在对象的属性中将 Json 查询为字符串

问题描述

我有这个:

public partial class spGetProductsFilter_Result1
{
    public int P_Id { get; set; }
    public string P_JsonData { get; set; }
    public Nullable<int> P_SC_Id { get; set; }
    public string P_Title { get; set; }
    public int SC_Id { get; set; }
    public Nullable<int> SC_C_Id { get; set; }
    public string SC_Name { get; set; }
    public string SC_Image { get; set; }
    public int C_Id { get; set; }
    public string C_Name { get; set; }
    public string C_Image { get; set; }
}

如您所见,json(P_JsonData)是字符串格式,例如,当颜色=“#FFC0CB”时,我需要过滤掉带有json的数组,所以在我的So中,如果这种颜色存在于json中,例如pNum = 2 的颜色为“#FFC0CB”,然后我想返回 spGetProductsFilter_Result1 对象。

使用 linq 如何做到这一点?

所以

 data = ctx.Database.SqlQuery<spGetProductsFilter_Result1>("exec spXXXXXX @cId, @scId", cid, scId).ToList();

 data = data.Where(?queryJson?)


var cid = new SqlParameter("@cId", Request.QueryString["cId"].ToString().TrimStart(',').TrimStart(','));
                var scId = new SqlParameter("@scId", Request.QueryString["scId"].ToString().TrimEnd(',').TrimStart(','));
                data = ctx.Database.SqlQuery<spGetProductsFilter_Result1>("exec spxxxxxx @cId, @scId", cid, scId).ToList();

                JavaScriptSerializer js2 = new JavaScriptSerializer();
                js.MaxJsonLength = 2147483644;

                List<MainProductObj> mpo = new List<MainProductObj>();
                foreach (spGetProductsFilter_Result1 r in data)
                {
                    if (r.P_Title.Length > 0)
                    {

                        MainProductObj mo = new MainProductObj()
                        {
                            pId = r.P_Id,
                            title = r.P_Title,
                            cId = Convert.ToInt32(r.P_SC_Id),
                            m = js.Deserialize<MainProduct>(r.P_JsonData)
                        };
                        mpo.Add(mo);
                    }
                }

我试过了:

 data = data.Where(x => ((JObject) js.Deserialize<MainProduct>(x.P_JsonData).pProds.Where(x => x.pColor == "#FFC0CB"))).ToLIst();

但没有运气

Json 在 P_JsonData 属性/变量中:

{
"pType": "2",
"pTitle": "A new Top",
"pProds": [{
    "formM": 1,
    "sDesc": "\u003cp\u003eA new Top\u003cbr\u003e\u003c/p\u003e",
    "lDesc": "\u003cp\u003eA new Top\u003cbr\u003e\u003c/p\u003e",
    "pColor": "#FFC0CB",
    "pSize": "L",
    "postage": "1",
    "quatity": 1,
    "aPrice": "1",
    "rPrice": "1",
    "Discounted": "1",
    "Price": "1",
    "p_Num": "4fa0479fae474596919f8e3e0249c515",
    "images": [{
        "mN": 1,
        "idImage": "image1",
        "fileName": "29cce29ef9c64624be5b920416ba45ef.jpg",
    }]
}, {
    "formM": 0,
    "sDesc": "",
    "lDesc": "",
    "pColor": "",
    "pSize": "0",
    "postage": "0",
    "quatity": 0,
    "aPrice": "0",
    "rPrice": "0",
    "Discounted": "0",
    "Price": "0",
    "p_Num": "39576e4beb554767b33c8bae1883cb01",
    "images": []
}]
}

标签: c#jsonlinq

解决方案


if (Request.QueryString["color"] != null)
                {
                    string lastData = "";
                    if(Request.QueryString["color"] !=  null)
                    {
                        lastData = Request.QueryString["color"].ToString().Replace("black", "#000000").Replace("white", "#ffffff");
                    }

                    data = data.Where(x => JsonConvert.DeserializeObject<MainProduct>(x.P_JsonData).pProds.Any(y => lastData.Split(',').Contains(y.pColor))).ToList();
                }

推荐阅读