gpt4 book ai didi

entity-framework - EF4 如何在 LINQ 中将匿名类型转换为强类型

转载 作者:行者123 更新时间:2023-12-03 23:38:26 26 4
gpt4 key购买 nike

LINQ 代码返回匿名类型如何返回强类型的“客户”?
我正在返回一个匿名类型,因为我只想从实体中选择某些字段。

var customer = from c in _entities.Customers
join con
in _entities.Contracts on c.CustomerID equals con.customerID
where con.contractNo == number
select new
{
Surname = c.Surname,
Forename= c.Forename,
Address = c.Address,
Suburb = c.Suburb,
State = c.State,
Postcode = c.PostCode,
PhoneNo = c.PhoneNo
};

谢谢

最佳答案

要么做

var customer = from c in _entities.Customers
join con
in _entities.Contracts on c.CustomerID equals con.customerID
where con.contractNo == number
select c;

按原样选择客户实例或
Customer customer = (from c in _entities.Customers
join con
in _entities.Contracts on c.CustomerID equals con.customerID
where con.contractNo == number
select new Customer{
Surname = c.Surname,
Forename= c.Forename,
Address = c.Address,
Suburb = c.Suburb,
State = c.State,
Postcode = c.PostCode,
PhoneNo = c.PhoneNo
}).FirstOrDefault();

仅填写您感兴趣的属性来创建客户的新实例。 (提供的客户类有一个无参数的构造函数)

关于entity-framework - EF4 如何在 LINQ 中将匿名类型转换为强类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com