[英]Converting a LINQ dynamic query to IQueryable in Entity Framework
I am trying to convert a LINQ query that joins two tables to IQueryable
type in Entity Framework so I can further apply filter expression on it. 我正在尝试将连接两个表的LINQ查询转换为Entity Framework中的
IQueryable
类型,因此我可以对其进一步应用过滤器表达式。
Here's the code: 这是代码:
IQueryable<Bill_Joined_V1> bills =
(from primary in _billEntity.BillTable
join secondary in _billEntity.BillInfoTable on primary.BillID equals secondary.BillID
orderby primary.BillID
select new
{
primary.BillID,
primary.CustomerID,
secondary.Name,
secondary.Value
}).AsQueryable<Bill_Joined_V1>().Where(FilterExp);
The entity classes are as follows: 实体类如下:
public class Bill_V1
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid BillID { get; set; }
public string BillTypeID { get; set; }
public Guid CustomerID { get; set; }
public Guid UserID { get; set; }
[ForeignKey("UserID")]
public virtual User_V1 UserTable { get; set; }
public double AmountDue { get; set; }
}
public class BillInfo_V1
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long BillInfoID { get; set; }
public Guid BillID { get; set; }
[ForeignKey("BillID")]
public virtual Bill_V1 BillTable { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
// Dummy bill record for query purpose
public class Bill_Joined_V1
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long BillRecID { get; set; }
public Guid BillID { get; set; }
[ForeignKey("BillID")]
public virtual Bill_V1 BillTable { get; set; }
public Guid CustomerID { get; set; }
[ForeignKey("CustomerID")]
public virtual Customer_V1 CustomerTable { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
The code does not compile. 该代码无法编译。 It produces error message saying
它产生错误信息说
'System.Linq.IQueryable' does not contain a definition for 'AsQueryable' and the best extension method overload 'System.Linq.Queryable.AsQueryable(System.Collections.Generic.IEnumerable)' has some invalid arguments
'System.Linq.IQueryable'不包含'AsQueryable'的定义,最佳扩展方法重载'System.Linq.Queryable.AsQueryable(System.Collections.Generic.IEnumerable)'具有一些无效的参数
Is there a way to convert the LINQ query to IQueryable
type without putting the query result to a physical table? 有没有一种方法可以将LINQ查询转换为
IQueryable
类型而不将查询结果放入物理表? How do I work around this? 我该如何解决? Thanks.
谢谢。
You shouldn't need to call .AsQueryable()
. 您不需要调用
.AsQueryable()
。 .Where()
is designed to return an IQueryable of appropriate type. .Where()
用于返回适当类型的IQueryable。 But the exact type of IQueryable in your code won't be IQueryable<Bill_Joined_V1>
because you are using an anonymous type. 但是您代码中IQueryable的确切类型将不是
IQueryable<Bill_Joined_V1>
因为您使用的是匿名类型。 What you really need to do is not use an anonymous type here: 您真正需要做的是不在此处使用匿名类型:
select new
{
primary.BillID,
primary.CustomerID,
secondary.Name,
secondary.Value
}).
Instead, do 相反,做
select new Bill_Joined_V1 { /* initialize properties */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.