C#迭代和插值语法

时间:2018-04-15 19:18:41

标签: c# linq

我被一个C#项目抛到了深处。我是Entity Framework,LINQ查询,Visual Studio,ASP.NET MVC和C#的新手。如果这是Ruby或JavaScript我可以做到没问题。

我需要一些帮助来返回所需信息的语法。我有完成2/3的逻辑,但我不知道如何迭代我的数据并正确显示它。

TL; DR - 我如何遍历数组(list1,list2,list3)并将其返回如下?

"#{asset} violates the rules."

以下是我需要满足的要求:

  • pc date> = da date& da date> = afl date& afl date> =提案日期。 仅当资产类别为开发(= 1)且pc日期,日期,afl日期和提议日期不为空时才应用此规则。
  • 资产没有提供所有必填字段(标题,州,基金,gav,gla和郊区)。
  • 每项资产的所有资金所有权总额应为100%。

代码:

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我简化了所以我可以测试。这包括迭代和插值字符串。要使用Debug.Print(出现在立即窗口中),请添加

using System.Diagnostics;

到文件的顶部。

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>();
        }