简体   繁体   中英

C# iteration and interpolation syntax

I got thrown into the deep end with a C# project. I'm brand new to Entity Framework, LINQ queries, Visual Studio, ASP.NET MVC, and C#. If this was Ruby or JavaScript I could do it no problem.

I need some help with the syntax for returning the required information. I have the logic about 2/3 completed, but I am just not sure how to iterate through my data and display it properly.

TL;DR - how do I iterate through the arrays (list1, list2, list3) and return it as the following?

"#{asset} violates the rules."

Here are the requirements I need to fulfill:

  • pc date >= da date & da date >= afl date & afl date >= proposal date. Apply this rule only if asset category is development(=1) and pc date, da date, afl date and proposal date are not null.
  • Asset didn't give all required fields (title, state, fund, gav, gla, and suburb).
  • The sum of ownership of all funds should be 100% for each asset.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Program.EFServices
{
    public class DataAuditService
    {
        public static List<DIM_Assets> getAssets()
        {
            var context = new radiusEntities();
            var query1 = from l in context.DIM_Assets
                         where ((l.category_id.ToString() == "1" /* category is development (1) */
                                && (l.pc_date < l.da_date || l.da_date < l.afl_date || l.afl_date < l.proposal_date)
                                && (l.pc_date == null || l.da_date == null || l.afl_date == null || l.proposal_date == null)
                               ))
                         select l;

            var query2 = from l in context.DIM_Assets
                         where ((l.category_id.ToString() == "1" /* category is development (1) */
                                && /* sum of ownership = 100% */
                                (l.FT_Ownership.Sum(f => f.percent) != 100)
                               ))
                         select l;

            var query3 = from l in context.DIM_Assets
                         where ((l.category_id.ToString() == "1" /* category is development (1) */
                                 && (l.state_id == null || l.GAV == null || l.GLA == null || l.SUBURB == null || l.ASSET == null || l.FT_Ownership.Count == 0))
                               )
                         select l;

            var list1 = query1.ToList();
            var list2 = query2.ToList();
            var list3 = query3.ToList();
        }
    }
}

I simplified so I could test. This covers the iteration and an interpolated string. To use Debug.Print (appears in the Immediate Window) add a

using System.Diagnostics;

to the top of the file.

private List<Coffee> coffees = new List<Coffee>();
private void btnDisplay_Click(object sender, EventArgs e)
        {
            List<Coffee> cofList = GetCoffeeList();
            foreach (Coffee c in cofList)
            {
                Debug.Print($"{c.CoffeeName} roaster's ID is {c.RoasterId}");
            }
        }
        private List<Coffee> GetCoffeeList()
        {
            var query = from c in coffees
                        where (c.RoasterId == 1)
                        select c;
            return query.ToList<Coffee>();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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