如何修改linq结果?

时间:2015-10-02 15:47:00

标签: c# asp.net linq

我有一个查询来获取数据,然后我将此数据转换为Json对象。 但是在获得结果以获得正确的数据之后我需要做一些改变。例如,我需要将我的“MainImageAddress”字段从X.com更改为XX.com。 我需要一个直接的解决方案,而不是使用Converting to datatable!

这是我的代码

var ThisArticle = (from Article in DarunamaEntity.ArticleNews
                                 where Article.IdArticleNews == ID_int
                                 select new
                                 {

                                     AID = Article.IdArticleNews,
                                     Views = Article.CountUserVisit,
                                     TextTitle = Article.TilteTextNews,
                                     MainImageAddress = Article.ImageUrlMain,
                                     Brief = Article.brief,
                                     Content = Article.BodyTextNews
                                 });

            JsonExport = serials.Serialize(ThisArticle);

---更新 -

我测试了这段代码:

var ThisArticle = (from Article in DarunamaEntity.ArticleNews
                                 where Article.IdArticleNews == ID_int
                                 select new
                                 {
                                     //~
                                     AID = Article.IdArticleNews,
                                     Views = Article.CountUserVisit,
                                     TextTitle = Article.TilteTextNews,
                                     MainImageAddress = convertToValidURL(Article.ImageUrlMain),
                                     //ThumbImage = Article.thumbUrl,
                                     Brief = Article.brief,
                                     Content = Article.BodyTextNews
                                 }).ToList().Select(Article=> new {
                                    AID = Article.AID,
                                    Views = Article.Views,
                                    TextTitle = Article.TextTitle,
                                    MainImageAddress = convertToValidURL(Article.MainImageAddress),
                                    Brief = Article.Brief,
                                    Content = Article.Content
                                 });

            JsonExport = serials.Serialize(ThisArticle);

这是我的convertToValidURL方法:

 public static string convertToValidURL(string URLImage)
    {

        if (URLImage == "" || URLImage == null)
        {
            return "";
        }
        URLImage = URLImage.Replace("~", "");
        URLImage = "http://www.darunama.com" + URLImage;
        int MessageCount = 0;


        return URLImage;
    }

但我收到此错误:

System.NotSupportedException:LINQ to Entities无法识别方法'System.String convertToValidURL(System.String)'方法,并且此方法无法转换为商店表达式。

2 个答案:

答案 0 :(得分:1)

您可以创建一个私有方法并在LINQ表达式中调用它。

var ThisArticle = (from a in DarunamaEntity.ArticleNews where a.IdArticleNews == ID_int
                 select new  {    
                               AID = a.IdArticleNews,
                               Views = a.CountUserVisit,
                               TextTitle = a.TilteTextNews,
                               MainImageAddress =a.ImageUrlMain,
                               Brief = a.brief,
                               Content = a.BodyTextNews
                             })
                            .AsEnumerable()
                            .Select(x=> new YourDto
                             {
                               AID = x.IdArticleNews,
                               MainImageAddress = TransformToWhatyouWant(x.ImageUrlMain)
                              // Fill in the remaining properties as well
                              }).ToList();

将此方法添加到同一个类

public string TransformToWhatyouWant(string imageUrlMain)
{
  //do what you want to do and return the updated string
  return "update your string and return here"; 
}

答案 1 :(得分:0)

var ThisArticle = (from Article in DarunamaEntity.ArticleNews
    where Article.IdArticleNews == ID_int
    select new
    {
        AID = Article.IdArticleNews,
        Views = Article.CountUserVisit,
        TextTitle = Article.TilteTextNews,
        MainImageAddress = Article.ImageUrlMain,
        Brief = Article.brief,
        Content = Article.BodyTextNews
    })
    .ToList()  // Convert from LINQ over SQL to LINQ over Objects
    .Select(Article=> new {
        AID = Article.IdArticleNews,
        Views = Article.CountUserVisit,
        TextTitle = Article.TilteTextNews,
        MainImageAddress = TransformThis(Article.ImageUrlMain),
        Brief = Article.brief,
        Content = Article.BodyTextNews
    });

JsonExport = serials.Serialize(ThisArticle);