【问题标题】:What is the right syntax for this joined EF Linq query这个加入的 EF Linq 查询的正确语法是什么
【发布时间】:2016-03-11 01:15:28
【问题描述】:

我正在尝试获取一个查询,该查询返回为我的 ViewModel 正确格式化的所有内容,因此我不必从我的实体模型中手动复制所有内容。我有这个 Linq 查询,它给了我一个错误。 :

var query = from i in context.Invoices 
            join l in context.LineItems on i.InvoiceID equals l.InvoiceID into il
            where i.InvoiceID == id
            select new InvoiceViewModel()
                {
                InvoiceID = i.InvoiceID,
                CustomerID = i.CustomerID,
                InvoiceNote = i.Note,
                InvoiceDate = i.InvoiceDate,
                Terms = i.Terms,
                LineItems = il.ToList<LineItemViewModel>()
                };

这是我的视图模型

 public class InvoiceViewModel        {
    public int InvoiceID { get; set; }
    public int CustomerID { get; set; }
    public string InvoiceNote { get; set; }
    public DateTime InvoiceDate { get; set; }
    public string Terms { get; set; }
    public virtual ICollection<LineItemViewModel> LineItems { get; set; }
    }

public class LineItemViewModel        {
    public int LineItemID { get; set; }
    public int InvoiceID { get; set; }
    public int Quantity { get; set; }
    public string Item { get; set; }
    public decimal Amount { get; set; }
    public string LineItemNote { get; set; }
    }

我得到的错误是(红色波浪线在 LineItems = il.ToList() 的 il 下)

'IEnumerable<LineItem>' does not contain a definition for 'ToList' and the best extension method overload 'Enumerable.ToList<LineItemViewModel>(IEnumerable<LineItemViewModel>)' requires a receiver of type 'IEnumerable<LineItemViewModel>'

我(有点,有点,有点)明白。那么正确的语法是什么?

【问题讨论】:

  • LineItems = il.select new LineItemViewModel() { LineItemID = il.LineItemID, etc }

标签: asp.net-mvc entity-framework linq asp.net-mvc-viewmodel


【解决方案1】:

您需要从 LineItem 实体显式初始化您的 LineItemViewModel 实例。你最好把它写成一个相关的子查询而不是一个连接:

var query = 
        from i in context.Invoices 
        where i.InvoiceID == id
        select new InvoiceViewModel()
        {
            InvoiceID = i.InvoiceID,
            CustomerID = i.CustomerID,
            InvoiceNote = i.Note,
            InvoiceDate = i.InvoiceDate,
            Terms = i.Terms,
            LineItems = 
            (
                from li in context.LineItems
                where li.InvoiceID == i.InvoiceID
                select new LineItemViewModel
                {
                    LineItemID = li.LineItemID, 
                    InvoiceID = li.InvoiceID,
                    Quantity = li.Quantity,
                    Item = li.Item,
                    Amount = li.Amount,
                    LineItemNote = li.LineItemNote,
                }
            ).ToList()
        };

【讨论】:

  • 这正是我所需要的。谢谢!我是一个长期从事 SQL 工作的人(回到 dBase II),学习这些 Linq 和模型的时间是原来的两倍,因为我首先要忘掉的东西太多了。
  • @BradMathews:有一个学习曲线,但我发现它是有回报的,因为 LINQ 利用了 C# 的强大类型特性,为您提供了比 T-SQL 更好的编译时类型检查和 IntelliSense。如果您想简化代码(以牺牲一些层分离为代价),您可以避免使用视图模型并直接投影数据模型。
  • 从我目前了解到的情况来看,后端直接使用数据模型更简单,但我不得不说我爱上了视图模型,因为它增加了简单性和电源前端。获得使用 Linq 的能力将是连接这一切的关键,你在这方面帮助了我。谢谢。
猜你喜欢
  • 2023-01-18
  • 1970-01-01
  • 2017-02-05
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多