如何获取IQueryable的存储过程?
问题描述:
我想从实体我的存储过程的情况下,我可以使用这样的:如何获取IQueryable的存储过程?
var WagonQuery = db.spGetTravelBYRTravelInfo(DepartureDate, FromId, ToId);
if (WagonId != 0)
WagonQuery = WagonQuery.Where(p => p.WagonID == WagonId); // HERE IS ERROR. Wagonquery Can Not Uses Like forexample List,So I Can Filter It And Use Several Where Clauses
grdTravels.DataSource = WagonQuery;
grdTravels.DataBind();
以下是错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Data.Objects.ObjectResult'. An explicit conversion exists (are you missing a cast?)
答
我应该将它转换到列表中,然后(就像这):
List<spGetTravelBYRTravelInfo_Result> lst = db.spGetTravelBYRTravelInfo(DepartureDate, FromId, ToId).ToList();
var dt = lst.Select(p => p);
if (WagonId != 0)
dt = dt.Where(p => p.WagonID == WagonId);
if (ddlReserved.SelectedValue == "t")
dt = dt.Where(p => p.Presell > 0);
答
我找不出你的存储过程返回。在使用EF 6.1.3通过做以下我的项目,你可以从数据库
List<SparePart> spSpares = db.Database.SqlQuery<SparePart>("spGetSparePartsFromProductId " + Convert.ToString(productId) + "," + Convert.ToString(subProduct));
在哪里我的模型是
public partial class SparePart
{
public long ItemID { get; set; }
public string ItemCode { get; set; }
public string ItemName { get; set; }
public Nullable<decimal> ItemPrice { get; set; }
public bool IsActive { get; set; }
public string ItemDesc { get; set; }
public Nullable<int> ItemGroup { get; set; }
public string Model { get; set; }
public string MajorCategory { get; set; }
public string MinorCategory { get; set; }
public string UOM { get; set; }
}
和产生相同的存储过程
CREATE PROC [dbo].[spGetSparePartsFromProductId]
(
@productId bigint,
@subproductId bigint=0
)
AS
BEGIN
if @productId=1
select * from SpareParts
END
GO
动态筛选列表as SpareParts表
可能会有所帮助