Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.Net中使用Lambda表达式进行Html解码_C#_.net_Lambda_Linq To Entities - Fatal编程技术网

C# 在.Net中使用Lambda表达式进行Html解码

C# 在.Net中使用Lambda表达式进行Html解码,c#,.net,lambda,linq-to-entities,C#,.net,Lambda,Linq To Entities,我试图在lambda表达式中解码HTML,但出现以下错误: LINQ to实体无法识别方法“System.String HtmlDecode(System.String)”方法,此方法无法转换为存储表达式 我使用的代码如下: OtherUserInformation userData = db.Users .Where(u => u.UserID.Equals(inputUserid)) .Select(x => new OtherUserInformation

我试图在lambda表达式中解码HTML,但出现以下错误:

LINQ to实体无法识别方法“
System.String HtmlDecode(System.String)
”方法,此方法无法转换为存储表达式

我使用的代码如下:

OtherUserInformation userData = db.Users
    .Where(u => u.UserID.Equals(inputUserid))
    .Select(x => new OtherUserInformation
    {
        Address = x.location == null ? "" : x.location,

        UserPic = db.UserProfile
            .Where(u => u.UserID.Equals(x.UserID))
            .Select(y => y.profileImg)
            .FirstOrDefault() == null 
                ? profileImagePath + "140_profile_default.jpg" 
                : profileImagePath + "140_" + db.UserProfile
                    .Where(u => u.UserID.Equals(x.UserID))
                    .Select(y => y.profileImg)
                    .FirstOrDefault(),
        CoverPic = db.UserProfile
            .Where(u => u.UserID.Equals(x.UserID))
            .Select(y => y.CoverImg)
            .FirstOrDefault() == null 
                ? coverImagePath + "812_cover_default.jpg" 
                : coverImagePath + coverPicPrefix + db.UserProfile
                    .Where(u => u.UserID.Equals(x.UserID))
                    .Select(y => y.CoverImg)
                    .FirstOrDefault(),
        Detail = db.UserProfile
            .Where(u => u.UserID.Equals(x.UserID))
            .Select(y => y.About).FirstOrDefault() == null 
                ? "" 
                : WebUtility.HtmlDecode(db.UserProfile
                    .Where(u => u.UserID.Equals(x.UserID))
                    .AsEnumerable()
                    .Select(y => y.About)
                    .FirstOrDefault()),
        FollowerCount = db.FollowUser
            .Where(u => u.FriendId.Equals(x.UserID) && u.FollowStatus.Equals(1))
            .Count(),
        Name = x.FirstName + " " + x.LastName,
        IsFollow = db.FollowUser
            .Where(u => u.UserId.Equals(userAuthInfo.UserId) 
                && u.FriendId.Equals(inputUserid) 
                && u.FollowStatus.Equals(1))
            .Select(z => z)
            .FirstOrDefault() == null ? "False" : "True",
    })
    .FirstOrDefault();
在上面的代码中,我需要包含HTML编码数据的“Detail”字段。但我无法用lambda表达式解码html。
任何帮助都将不胜感激。

请尝试HttpUtility.HtmlDecode方法

或者按照下面的示例尝试解码Select方法中的字段:

OtherUserInformation userData = db.Users.Where(u => u.UserID.Equals(inputUserid)).Select(x => new OtherUserInformation
                    {
                        Address = x.location == null ? "" : x.location,
                       // UserPic = Utilities.ImagePathForProfileForUserByDiviceType(db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).Select(y => y.profileImg).FirstOrDefault(),deviceType),
                       // CoverPic = Utilities.ImagePathForCoverImageForUserByDiviceType(db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).Select(y => y.CoverImg).FirstOrDefault(), deviceType),

                        UserPic = db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).Select(y => y.profileImg).FirstOrDefault() == null ? profileImagePath + "140_profile_default.jpg" : profileImagePath + "140_" + db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).Select(y => y.profileImg).FirstOrDefault(),
                        CoverPic = db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).Select(y => y.CoverImg).FirstOrDefault() == null ? coverImagePath + "812_cover_default.jpg" : coverImagePath + coverPicPrefix + db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).Select(y => y.CoverImg).FirstOrDefault(),
                        Detail = db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).Select(y => y.About).FirstOrDefault() == null ? "" : db.UserProfile.Where(u => u.UserID.Equals(x.UserID)).AsEnumerable().Select(y => WebUtility.HtmlDecode(y.About)).FirstOrDefault(),
                        FollowerCount = db.FollowUser.Where(u => u.FriendId.Equals(x.UserID) && u.FollowStatus.Equals(1)).Count(),
                        Name = x.FirstName + " " + x.LastName,
                        IsFollow = db.FollowUser.Where(u => u.UserId.Equals(userAuthInfo.UserId) && u.FriendId.Equals(inputUserid) && u.FollowStatus.Equals(1)).Select(z => z).FirstOrDefault() == null ? "False" : "True",


                        })


                    }).FirstOrDefault();

在解析查询之前,不能使用该方法或类似方法,因为sql对该程序集一无所知。如果不是EntityFunction,则必须在解析后重新投影列表。如果您使用像AutoMapper这样的数据映射器,您可以在重投影中使用它并在那里进行调用,但如果需要手动进行,请在调用toList()后再进行调用


我得到了解决方案,并在上述查询的底部添加了几行代码,如下所示:

OtherUserInformation userData = db.Users
    .Where(u => u.UserID.Equals(inputUserid))
    .Select(x => new OtherUserInformation
    {
        Address = x.location == null ? "" : x.location,

        UserPic = db.UserProfile
            .Where(u => u.UserID.Equals(x.UserID))
            .Select(y => y.profileImg)
            .FirstOrDefault() == null 
                ? profileImagePath + "140_profile_default.jpg" 
                : profileImagePath + "140_" + db.UserProfile
                    .Where(u => u.UserID.Equals(x.UserID))
                    .Select(y => y.profileImg)
                    .FirstOrDefault(),
        CoverPic = db.UserProfile
            .Where(u => u.UserID.Equals(x.UserID))
            .Select(y => y.CoverImg)
            .FirstOrDefault() == null 
                ? coverImagePath + "812_cover_default.jpg" 
                : coverImagePath + coverPicPrefix + db.UserProfile
                    .Where(u => u.UserID.Equals(x.UserID))
                    .Select(y => y.CoverImg)
                    .FirstOrDefault(),
        Detail = db.UserProfile
            .Where(u => u.UserID.Equals(x.UserID))
            .Select(y => y.About).FirstOrDefault() == null 
                ? "" 
                : WebUtility.HtmlDecode(db.UserProfile
                    .Where(u => u.UserID.Equals(x.UserID))
                    .AsEnumerable()
                    .Select(y => y.About)
                    .FirstOrDefault()),
        FollowerCount = db.FollowUser
            .Where(u => u.FriendId.Equals(x.UserID) && u.FollowStatus.Equals(1))
            .Count(),
        Name = x.FirstName + " " + x.LastName,
        IsFollow = db.FollowUser
            .Where(u => u.UserId.Equals(userAuthInfo.UserId) 
                && u.FriendId.Equals(inputUserid) 
                && u.FollowStatus.Equals(1))
            .Select(z => z)
            .FirstOrDefault() == null ? "False" : "True",
    })
    .FirstOrDefault();
}).FirstOrDefault().AsEnumerable().Select(x=>new OtherUserInformation {

                        Address = x.Address,
                        UserPic = x.UserPic,
                        CoverPic = x.CoverPic,
                        Detail = Regex.Replace(x.Detail, "<.*?>", string.Empty),
                        FollowerCount = x.FollowerCount,
                        Name = x.Name,
                        IsFollow = x.IsFollow,
                        Categories = x.Categories,
                        Followers = x.Followers

                    }).FirstOrDefault();
Address=x.地址,
UserPic=x.UserPic,
CoverPic=x.CoverPic,
Detail=Regex.Replace(x.Detail,“,string.Empty),
FollowerCount=x.FollowerCount,
Name=x.Name,
IsFollow=x.IsFollow,
类别=x.类别,
Followers=x.Followers
}).FirstOrDefault();

上述解决方案对我有效。快乐编码:)

这已经被问了很多次了,有很多不同的口味。我试过了,但我的问题没有得到任何满意的答案。不,这不是问题的可能重复。唯一的缺点是他的预测似乎大量使用了数据库。通过先枚举,投影可能会产生大量额外的数据库查询。不是这样,没有额外的数据库调用。一旦从数据库解析出列表,就会在内存中从一个对象到另一个对象进行另一个投影。我在这里使用Regex.Replace函数,但我们也可以使用HttpUtility.HtmlDecode函数。现在,我只是根据我的要求删除了所有HTML标记。