简体   繁体   English

如何存储Linq查询并在以后使用?

[英]How to store a Linq query and use it later?

I'm doing a wpf App and i've got a bit of an issue i would like to ask you about. 我正在做一个wpf应用程序,我想问一个问题。

I'm querying a database on the window level and i pass the result of the query to a method in my object like this : 我在窗口级别上查询数据库,并将查询结果传递给对象中的方法,如下所示:

Window level code : 窗口级代码:

payrollEmailManager.SetListOfSalariesToEmailTo(
    from Record in SqlInfo.SqlTable.T_SALs 
    where Record.EtatPaie == 3 
    select new {
        Matricule = Record.MatriculeSalarie, 
        Nom = Record.Nom,
        Prenom = Record.Prenom, 
        Email = Record.EMail });

This is my Method Definition : 这是我的方法定义:

public void SetListOfSalariesToEmailTo(object _ListOfSalaryToRecieveMail)
{
   ListOfSalary = _ListOfSalaryToRecieveMail;
}

Where ListOfSalary is also of type object . 其中ListOfSalary也是object类型。

Now here is the issue for me, I have another method where I want to go trough each record of listofsalary and get the information I selected in query like Matricule or Email , something like this : 现在这是我的问题,我有另一种方法,我想listofsalary每条记录,并获取我在查询中选择的信息,例如MatriculeEmail ,如下所示:

public void SendEmail()
{
    foreach(var Salary in (dynamic)ListOfSalary)
    {
        Mail.To.Add(Salary.????
    }
}

I can't reference the Nom column or the Email column any advice ?? 我不能在Nom列或Email列中引用任何建议?

If you consider your following query: 如果考虑以下查询:

var query = from Record in SqlInfo.SqlTable.T_SALs 
            where Record.EtatPaie == 3 
            select new { 
                Matricule = Record.MatriculeSalarie, 
                Nom = Record.Nom, 
                Prenom = Record.Prenom, 
                Email = Record.EMail 
            };

After running this line the query is not yet executed to database. 运行此行后,尚未对数据库执行查询。 Only when you materialize it (using functions like ToList() / ToArray() / FirstOrDefault etc.) it is actually being executed in the database and information is returned. 仅当您实现它时(使用ToList() / ToArray() / FirstOrDefault等功能),它实际上才在数据库中执行并返回信息。

Therefore if you just do SomeFunction(query); 因此,如果您只是执行SomeFunction(query); it does not execute the query and you can store it for later execution. 它不会执行查询,您可以将其存储以供以后执行。

However you do need to change your code a bit: 但是,您确实需要稍微更改代码:

  • The function should not get object but IQueryable<T> 该函数不应获取对象,而是IQueryable<T>

     public void SetListOfSalariesToEmailTo(IQueryable<T> query) 
  • As you want to store the query you need to later on know the type of each item. 当您要存储查询时,您以后需要知道每个项目的类型。 To do so do not use an anonymous object ( new { } ) in the select. 为此,请不要在选择中使用匿名对象( new { } )。 Use instead a custom object or use c# 7.0 named tuples and then the function will look like: 改用自定义对象或使用名为元组的c#7.0,然后函数将如下所示:

     var query = from Record in SqlInfo.SqlTable.T_SALs where Record.EtatPaie == 3 select new SomeType { Matricule = Record.MatriculeSalarie, Nom = Record.Nom, Prenom = Record.Prenom, Email = Record.EMail }; public void SetListOfSalariesToEmailTo(IQueryable<SomeType> query) { ListOfSalary = query; } 

You can still use object and dynamic as you did, and just access the properties, but you will not have the intellisense showing you the properties and options, as it does not know the concrete type. 您仍然可以像以前一样使用objectdynamic object ,并且仅访问属性,但是由于智能对象不知道具体的类型,因此您将不会具有智能提示来显示属性和选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM