从数据集返回类的 linq 查询

问题描述 投票:0回答:1

我需要帮助编写此 linq 查询。

我有这门课:

public class ClassProva
{
public int Id { get; set; }
public string Name { get; set; }
public ClassSeconda Seconda { get; set; } = new();
}


public class ClassSeconda
{
public string Nome { get; set; }
public double Maggiore { get; set; }
public double Minore { get; set; }
}

我想得到的查询是这样的:

ClassProva dati = (from c in DS.Tables[0].AsEnumerable()
where c.Field<Int32>("Ident") == Id
select new ClassProva ()
{
Name = c.Field<string>("Nome"),
Seconda.Nome = c.Field<string>("Nome_Seconda"),
Seconda.Maggiore = c.Field<double>("Maggiore"),
Seconda.Minore = c.Field<double>("Minore")
}).FirstOrDefault();

但似乎不可能做到这一点:

Seconda.Nome
linq
1个回答
0
投票

我相信您需要在

ClassSeconda
初始值设定项列表中显式新建一个
ClassProva
对象,然后使用嵌套的初始值设定项列表初始化其属性。

ClassProva dati = (
        from c in DS.Tables[0].AsEnumerable()
        where c.Field<Int32>("Ident") == Id
        select new ClassProva {
            Name = c.Field<string>("Nome"),
            Seconda = new ClassSeconda {
                Nome = c.Field<string>("Nome_Seconda"),
                Maggiore = c.Field<double>("Maggiore"),
                Minore = c.Field<double>("Minore")
            }
        }
    ).FirstOrDefault();
© www.soinside.com 2019 - 2024. All rights reserved.