从linq查询中选择一种新类型 [英] Selecting a new type from linq query

查看:71
本文介绍了从linq查询中选择一种新类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Linq转换从存储库中选择的一些数据.

I'm trying to transform some data selected out of a repository using Linq.

到目前为止,我的代码:

My code so far:

Repository<Result> _repository = new Repository<Result>();

var disciplines = _repository.Query()
    .Select(d => new Discipline
    {
        DisciplineCode = d.DisciplineCode,
        Name = d.DisciplineName
    })
    .Distinct()
    .ToList();

结果类如下:

public class Result
{
    public virtual int ResultId { get; set; }
    public virtual string DisciplineCode { get; set; }
    public virtual string DisciplineName { get; set; }
    public virtual int CompetitorId { get; set; }
    //other stuff
}

运行时,我得到

无法确定表达式的序列化信息: < MemberInitExpression>

Unable to determine the serialization information for the expression: < MemberInitExpression >

知道发生了什么事吗?

根据Chris的建议,我尝试了这样的ToList之后选择:

As per Chris suggestion, I tried the Select after ToList like this:

var disciplines = _repository.Query()
                .Select(d => new
                {
                    DisciplineCode = d.DisciplineCode,
                    Name = d.DisciplineName
                })
                .Distinct()
                .ToList()
                .Select(d => new Discipline { DisciplineCode = d.DisciplineCode, Name = d.Name });

但是,这一次,类似的错误,但与匿名类型有关:

However, this time, similar error, but it's to do with the anonymous type:

无法确定表达式的序列化信息:new __AnonymousType(d.DisciplineCode,d.DisciplineName).

Unable to determine the serialization information for the expression: new __AnonymousType(d.DisciplineCode, d.DisciplineName).

为澄清起见,.Query返回IQueryable
基础数据库是MongoDB(使用C#驱动程序)

To clarify, .Query is returning IQueryable
The underlying database is MongoDB (using C# driver)

如果我这样做:

var disciplines = _repository.Query()
    .Select(d => d.DisciplineName)
    .Distinct()
    .ToList()

有效.通过工作,我的意思是我得到了distinct DisciplineNames列表

It works. By works, I mean I get a distinct list of DisciplineNames

但是我需要能够选择的属性不仅仅是名称.

I need to be able to select more properties than just the name however.

推荐答案

我怀疑您的问题是MongoDB驱动程序不知道如何创建Discipline对象(或与此相关的匿名对象).

I would suspect that your issue is that the MongoDB driver doesn't know how to create a Discipline object (or an anonymous object for that matter).

您需要移出IQueryable<>并移至IEnumerable<>才能进行此工作.

You need to move out of the the IQueryable<> and into IEnumerable<> to make this work.

尝试一下:

var disciplines =
    _repository
        .Query()
        .ToArray()
        .Select(d => new Discipline
            {
                DisciplineCode = d.DisciplineCode,
                Name = d.DisciplineName
            })
        .Distinct()
        .ToList();

.ToArray()是这里的魔力.让我知道这是否有效.

The .ToArray() is the magic here. Let me know if this works.

在您的自定义类型上,.Distinct()调用仍然可能存在问题,因此您可能需要尝试以下操作:

You still may have issues with the .Distinct() call working on your custom type, so you may need to try this:

var disciplines =
    _repository
        .Query()
        .ToArray()
        .Select(d => new
            {
                d.DisciplineCode,
                d.DisciplineName
            })
        .Distinct()
        .Select(d => new Discipline
            {
                DisciplineCode = d.DisciplineCode,
                Name = d.DisciplineName
            })
        .ToList();

这篇关于从linq查询中选择一种新类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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