使用linq在对象的属性中以字符串形式查询Json [英] Querying Json as string within a property of an object using linq

查看:280
本文介绍了使用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)是字符串格式,例如,当color =#FFC0CB"时,我需要过滤掉带有json的数组,因此在我 因此,如果json中存在这种颜色,例如pNum = 2的颜色为#FFC0CB",那么我想返回spGetProductsFilter_Result1对象.

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.

如何使用linq完成此操作?

How can this be done, using 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属性/变量中:

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

推荐答案

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

这篇关于使用linq在对象的属性中以字符串形式查询Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆