Querying Json as string within a property of an object using linq

二次信任 提交于 2020-06-29 05:11:31

问题


I have this:

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; }
}

As you can see that the json (P_JsonData) is in string format, i need to filter out the arrays withing the json when for example a color = "#FFC0CB", so in my So if this color exists in the json so for example pNum = 2 has a color of "#FFC0CB" then i want to return the spGetProductsFilter_Result1 object.

How can this be done, using linq?

so

 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);
                    }
                }

I tried:

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

but no luck

Json is here within P_JsonData property / variable:

{
"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": []
}]
}

回答1:


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();
                }


来源:https://stackoverflow.com/questions/62311942/querying-json-as-string-within-a-property-of-an-object-using-linq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!