我不确定我是否正确。我尝试只选择从 LINQ 查询中返回的几个项目。最终我想收回大部分信息,但有些字段是 FK ID,所以我必须从其他表中获取它们的名称值。
我现在遇到的问题是从匿名到账单的列表转换。我该如何解决这个问题?
public class BillDO
{
/// <summary>
/// Returns all user bills based on their UserName
/// </summary>
/// <param name="UserName"></param>
/// <returns></returns>
public List<Bill> GetBills(string UserName)
{
BillsEntities be = new BillsEntities();
var q = from b in be.Bills
where b.UserName == UserName
orderby b.DueDate
select new { b.DueDate, b.Name, b.PaymentAmount, b.URL };
List<Bill> billList = q.ToList();
return billList;
}
}
最佳答案
我们不能确定,但我怀疑你可以这样做:
var q = from b in be.Bills
where b.UserName == UserName
orderby b.DueDate
select new { b.DueDate, b.Name, b.PaymentAmount, b.URL };
return q.AsEnumerable()
.Select(b => new Bill { DueDate = b.DueDate,
Name = b.Name,
PaymentAmount = b.PaymentAmount,
URL = b.URL })
.ToList();
换句话说,从数据库中提取匿名类型的数据,然后返回“手动创建的实体”列表,并复制相关属性。
关于.net - 无法将 Generic.List<AnonymousType#1> 转换为 Generic.List<BillEF.Bill>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418345/