简体   繁体   English

用Linq代替foreach

[英]Substituting foreach in linq

I have a query that I want to omit foreach and make the whole in a query 我有一个查询,我想省略foreach并在查询中进行整体

I want to know how to implement both foreach and all the if clauses after the query inside the query? 我想知道如何在查询内部的查询之后实现foreach和所有if子句?

Thanks for the help :) 谢谢您的帮助 :)

foreach (var item in Filter.EPN)
                {
                    string H = item.Substring(0, 11);
                    string VH = item.Substring(14, 2);
                    string S = item.Substring(19, 11);
                    string VS = item.Substring(33);
                    ret1 = (from niv in Cniv
                            from dea in Dniv
                                .Where(d => niv.niv == d.niv)
                            let ccc = CTic.FirstOrDefault(c => c.Tic == niv.SMF || c.Tic == dea.SM)
                            join list in ListM on ccc.mar equals list.marE
                            where dea.time >= Filter.From && dea.time <= Filter.To &&
                                     niv.H == H && niv.VH == VH && niv.S == S && niv.VS == VS &&
                                     dea.ModelCode == Filter.Model
                            select new ReportData
                                 {
                                     niv = niv.niv,
                                     Ac = niv.Ac,
                                 });
                    if (Filter.mar != null && Filter.mar.Count > 0)
                    {
                        ret1 = ret1.Where(z => Filter.mar.Contains(z.mar));
                    }
                    if (ret1 != null && ret1.ToList().Count > 0)
                    {
                        ret.AddRange(ret1);
                    }
                }

You can create a SQL function that will accept the parameters that you want to add, so instead of a view create a function that will accept parameters and you can call that with linq. 您可以创建一个将接受要添加的参数的SQL函数,因此可以代替视图创建一个可以接受参数的函数,并可以使用linq进行调用。

Check out the article here: https://msdn.microsoft.com/en-us/library/bb386954(v=vs.110).aspx 在此处查看文章: https : //msdn.microsoft.com/zh-cn/library/bb386954(v=vs.110).aspx

So what'll you basically do is create the function that would accept a string for the market and return the values back. 因此,您基本上要做的就是创建一个函数,该函数将接受市场的字符串并返回值。 If you're looking for raw speed you could also do a sql command and get back a datatable. 如果您正在寻找原始速度,也可以执行sql命令并获取数据表。 If you're just displaying data this would be the easiest method. 如果您只是显示数据,这将是最简单的方法。

        string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connection);
        con.Open();
        var cmd = new SqlCommand("SELECT * FROM myTable where market like" + market variable, con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable dataTable = new DataTable();

Then you have a datatable with the results of your view which is fairly easy to manipulate. 然后,您将获得一个包含视图结果的数据表,该表很容易操作。

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

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