Linq转换

发布于 2024-08-18 04:37:33 字数 589 浏览 4 评论 0原文

我使用以下代码返回 IList:

public IList<string> FindCodesByCountry(string country)
        {
            var query = from q in session.Linq<Store>()
                        where q.Country == country
                        orderby q.Code
                        select new {q.Code};

            return (IList<string>) query.ToList();
        }

但是我不断收到此错误:

无法转换类型为 'System.Collections.Generic.List1[<>f__AnonymousType01[System. String]]' 以键入“System.Collections.Generic.IList`1[System.String]”。

我应该返回这里什么?

I am using the following code to return an IList:

public IList<string> FindCodesByCountry(string country)
        {
            var query = from q in session.Linq<Store>()
                        where q.Country == country
                        orderby q.Code
                        select new {q.Code};

            return (IList<string>) query.ToList();
        }

However I keep getting this error:

Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType01[System.String]]' to type 'System.Collections.Generic.IList`1[System.String]'.

What I am supposed to return here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静赏你的温柔 2024-08-25 04:37:33

只要 q.code 是一个字符串,这应该可以工作:
请注意,它并没有创建匿名对象,只是选择了字符串。

    public IList<string> FindCodesByCountry(string country)
    {
        var query = from q in session.Linq<Store>()
                    where q.Country == country
                    orderby q.Code
                    select q.Code;

        return query.ToList();
    }

as long as q.code is a string this should work:
note that it is not creating an anonymous object, just the string is being selected.

    public IList<string> FindCodesByCountry(string country)
    {
        var query = from q in session.Linq<Store>()
                    where q.Country == country
                    orderby q.Code
                    select q.Code;

        return query.ToList();
    }
爱殇璃 2024-08-25 04:37:33

您选择匿名类型有什么原因吗?如果没有尝试这个...

    var query = from q in session.Linq<Store>()
                where q.Country == country
                orderby q.Code
                select q.Code;

Is there a reason you were selecting an anonymous type? If not try this...

    var query = from q in session.Linq<Store>()
                where q.Country == country
                orderby q.Code
                select q.Code;
趁年轻赶紧闹 2024-08-25 04:37:33

怎么样

query.Select(s => s.ToString()).ToList();

Or

query.Cast<String>().ToList();

?但是我假设 q.Code 是一个字符串?在这种情况下,您只想更改 LINQ 表达式:

var query = from q in session.Linq<Store>()
                    where q.Country == country
                    orderby q.Code
                    select q.Code;

How about

query.Select(s => s.ToString()).ToList();

Or

query.Cast<String>().ToList();

But I'm assuming that q.Code is a string? In which case you just want to change your LINQ expression:

var query = from q in session.Linq<Store>()
                    where q.Country == country
                    orderby q.Code
                    select q.Code;
2024-08-25 04:37:33

在查询中,无需选择包含字符串的匿名类,只需选择字符串本身:

var query = from q in session.Linq<Store>()
            where q.Country == country
            orderby q.Code
            select q.Code;

In the query, instead of selecting an anonymous class containing a string, just select the string itself:

var query = from q in session.Linq<Store>()
            where q.Country == country
            orderby q.Code
            select q.Code;
薄荷梦 2024-08-25 04:37:33

您不能将自定义类型列表转换为这样的字符串列表。最简单的方法是让您的 query 对象以 iEnumerable 字符串列表(而不是自定义类型)的形式开始其生命周期。将您的选择行更改为:

select new q.Code.toString();

就可以了。如果 q.Code 本身就是一个字符串开头,则不需要 .ToString()

You can't cast a list of custom types to a list of strings like that. The easiest way would be to have your query object begin it's life as an iEnumerable list of strings, rather than a custom type. Change your select line to:

select new q.Code.toString();

and you'll be good. If q.Code is itself a string to begin with, then the .ToString() won't be necessary.

试试这个:

return query.ToList<string>();

Try this:

return query.ToList<string>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文