LINQ到实体的ToString() - 提出的解决方案的工作没有? [英] LINQ to Entities ToString() - None of the proposed solutions work?

查看:320
本文介绍了LINQ到实体的ToString() - 提出的解决方案的工作没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴这更多的是因为我想了解更多信息,因为我的工作,周围已经基本上避免使用LINQ到实体!它会很好,如果我可以,虽然使用它。因此,如果任何人都可以赐教。

I'm posting this more because I'd like to learn more, because my work-around has been to basically avoid using LINQ to Entities! It would nice if I could use it though. So if anyone can enlighten me..

我工作的一个ASP.Net MVC 3应用程序(我的第一个)使用code-第一实体框架作为我的数据模型。这将是一个在线商店,我的工作管理控制器,其中,作为管理员,您将能够编辑产品等。当谈到加入新的产品,你必须指定一个类别,这意味着类别的下拉列表。

I'm working on an ASP.Net MVC 3 application (my first one) using code-first Entity Framework as my data model. It's going to be an online shop and I am working on the 'Admin' controller where, as an admin, you will be able to edit products etc. When it comes to adding a new product, you must specify a category, which means a dropdown list of categories.

我开始通过只返回在ViewBag类别的IEnumerable集合的基础上,我发现了信息的此处。这似乎是一个非常好的方式来处理事情。

I started off by just returning an IEnumerable collection of categories in the ViewBag, based on the information I found here. This seemed like a really nice way to approach things.

但我得到你会看到下面的同样的错误。所以,以下的建议我在线阅读,我创建了一个单独的模型专门用于创造新的和编辑现有产品。该分类收集用于建立视图中的下拉列表。

But I got the same error you'll see below. So following a suggestion I read online, I created a separate model specifically for creating new and editing existing products. The Categories collection is used to build a dropdown list in the view.

public class ProductModel
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public bool IsVisible { get; set; }

    public IEnumerable<SelectListItem> Categories
    {
        get
        {
            return new DataAccess()
                .Categories
                .OrderBy(c => c.Description)
                .Select(c => new SelectListItem
                    {
                        Value = c.Id.ToString(),
                        Text = c.Description
                    });
        }
    }
}

这将导致以下错误信息: LINQ到实体无​​法识别方法'System.String的ToString()方法,而这种方法不能被翻译成店前pression 。一个建议的在线解决方案是使用实体框架提供的SqlFunction方法。所以,我想这不是...

This results in the following error message: "LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.". One proposed online solution was to use the SqlFunction methods provided by the Entity Framework. So I tried this instead ...

    public IEnumerable<SelectListItem> Categories
    {
        get
        {
            return new DataAccess()
                .Categories
                .OrderBy(c => c.Description)
                .Select(c => new SelectListItem
                    {
                        Value = SqlFunctions.StringConvert((double)c.Id),
                        Text = c.Description
                    });
        }
    }

不过,这将导致该错误消息:指定的方法'System.String StringConvert(System.Nullable`1 [System.Double])'的类型'System.Data.Objects.SqlClient.SqlFunctions不能转化为LINQ到实体店前pression。

我甚至尝试添加只读属性名为 IdAsString 数据类,但是这显然使得实体框架很不爽!

I've even tried adding a read-only property to the data class called IdAsString, but this obviously makes the Entity Framework very unhappy!

在最后,我意识到,这是不值得所有我把进去的时候,只是完全使用LINQ停止。这工作得很好,但它会很高兴地知道,如果有一个正确的方式做到这一点,实际工作!

In the end, I realised that it wasn't worth all the time I was putting into it, and just stopped using LINQ altogether. This works fine, but it would be nice to know if there is a "correct" way to do this that actually works!

这是我目前的工作的解决方案:

This is my current working solution:

    public IEnumerable<SelectListItem> Categories
    {
        get
        {
            var dal = new DataAccess();
            var categories = dal.Categories;
            var items = new List<SelectListItem>();

            foreach (var category in categories)
                items.Add(new SelectListItem
                    {
                        Value = category.Id.ToString(),
                        Text = category.Description
                    });

            return items;
        }
    }

所以,如果有人已经这样做的,他们认为他们正在做正确的方式,那么我很想知道你在想什么!

So if anyone is already doing this and they think they're doing it the "correct" way, then I'd love to know what you think!

推荐答案

物化查询调用的ToString将最后一部分非LINQ到实体查询之前。您可以通过仅选择ID和描述,然后调用AsEnumerable,然后得到selectlistitems优化这一点。

Materialize the query before you invoke ToString will make the last part a non linq-to-entities query. You could optimize this by only selecting the id and description, then calling AsEnumerable, and then getting the selectlistitems.

    return new DataAccess() 
        .Categories 
        .OrderBy(c => c.Description)
        .AsEnumerable()
        .Select(c => new SelectListItem 
            { 
                Value = c.Id.ToString(), 
                Text = c.Description 
            }); 

这篇关于LINQ到实体的ToString() - 提出的解决方案的工作没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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