gpt4 book ai didi

c# - 如何使用 Linq2Sql 构建我的服务/存储库代码!

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:20 27 4
gpt4 key购买 nike

我在构建我的应用程序时遇到了问题。

我有以下结构,只显示了重要的方面。

namespace Domain
{
public class Invoice
{
//properties
}

public class InvoiceRepository
{
public Linq2SqlContext context = new Linq2SqlContext();

public IQueryable<Invoice> GetInvoices()
{
var query = from inv in _dbctx.Invoices orderby inv.invInvoiceDate descending select GetInvoice(inv) ;

return query;

}
}
public class InvoiceService()
{
public InvoiceRepository _repository = new InvoiceRepositroy();
public IQueryable<Invoice> GetInvoices()
{
return _repository.GetInvoices();
}

}
}

namespace MyApp
{
public class UI
{
public InvoiceService _service = new InvoiceService();
public void FilterInvoices()
{
var query =
(
from i in _service.GetInvoices()
from s in _service.GetStatuses()
where i.ProjectID == _projectid &&
s.ID == i.Status
select new
{
InvoiceID = i.ID,
DocumentTotal = i.TotalDue.ToString(),
Created = i.Created,
WeekEnding = i.WeekEnding,
Status = s.staStatus
}
).Skip(_pageIndex * _pageSize).Take(_pageSize);

}

}
{

所以我想从我的服务中返回 IQueryable 这样我就可以从客户端代码中过滤。但我提出的问题FilterInvoices 方法错误是“不支持翻译成 sql"因为 GetInvoice 方法用于返回 Invoice 实体(这是LInq2 sql 层之上的一个层)而不是 Linq2sql 发票实体。

那么我如何使用这种结构从我的服务中返回一个 IQueryable?另外,我如何在存储库 GetInvoices 中排序和返回 IQureyable。

希望这是有道理的。

马尔科姆

最佳答案

linq2sql 认为 GetInvoice(在 GetInvoices 中)是一个存储过程。一种解决方法

var query = from inv in _dbctx.Invoices orderby inv.invInvoiceDate descending select inv ;

虽然这会传回由您的数据上下文生成的对象。如果您想填充自定义对象,您可以迭代创建自定义 Invoice 对象的集合并填充它们。

foreach(var inv in query) { somelist.Add(new MyCustomInvoince() { id = inv.id ... }

编辑:以上将返回一个列表。使用以下返回 IQueryable

return from item in query
select GetInvoice(item);

不同之处在于在这个阶段您使用的是 Linq2Objects,并且该提供者将知道如何调用 GetInvoice

关于c# - 如何使用 Linq2Sql 构建我的服务/存储库代码!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2316781/

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