【问题标题】:List based on variable from two tables基于两个表中的变量的列表
【发布时间】:2014-01-06 02:10:05
【问题描述】:

我的程序是一个任务程序。我想做的是为用户/员工构建一个 UI,以查看他们在登录当天必须执行的任务。

我在 1-M 中有两张表,PostOne 和 PostEig。

  • PostOne 是包含单个任务信息的主表。
  • PostEig 是在 Post One 中分配给任务的用户表。

模型[简化]

public class PostOne
{
    public string One { get; set; }
    [Key]
    public string Two { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime ThrD { get; set; }
}

public class PostEig
{
    public string EigOne { get; set; }
    public string EigTwo { get; set; } //foreign key
    [Key]
    public string EigID { get; set; }
    [Required]
    public string EigA { get; set; }  //user login 
}

我遇到了控制器问题。我什至不确定如何开始编写实现目标所需的代码,所以我将尝试将其写出来:

  1. 调用 PostEigs 列表,其中 EigA == User.Identity.Name
  2. 并从此列表中......调用 PostOnes 列表,其中两个 == EigTwo
  3. 并从此列表中......调用 PostOnes 列表,其中 ThrD == DateTime.UtcNow.Date

我确实尝试过这样的事情:

public ActionResult SkedList()
{
    return View(db.PostEigs.Where(m =>
            m.EigA == User.Identity.Name ||
            m.EigTwo == db.PostOnes.Where(o => o.ThrD == DateTime.UtcNow.Date)
            ).ToList());
}

如果不清楚,请告诉我。我感谢任何建议或解决方案,即使方向不同。

【问题讨论】:

    标签: c# linq asp.net-mvc-4 ef-code-first


    【解决方案1】:

    听起来这是Inner Join 的候选对象。我发现用 SQL 来思考然后将其转换为 LINQ 会容易得多。

    SQL 查询:

    SELECT 
        po.* 
    FROM 
        PostOnes po
    INNER JOIN 
        PostEig pe
    ON 
        pe.EigTwo = po.Two
    WHERE 
        pe.EigA = AUTH_NAME AND po.ThrD = TODAY()
    

    C# LINQ 查询:

    var DB_PostEig = new List<PostEig>()
    {
        new PostEig(){EigTwo = "Foo1", EigA =  "Foo"},
        new PostEig(){EigTwo = "Foo2", EigA =  "Foo"},
        new PostEig(){EigTwo = "Bar1", EigA =  "Bar"},
        new PostEig(){EigTwo = "Bar2", EigA =  "Bar"}
    };
    
    var DB_PostOnes = new List<PostOne>()
    {
        new PostOne(){Two = "Foo1", ThrD = new DateTime(1900,1,1)},
        new PostOne(){Two = "Foo2", ThrD = new DateTime(2000,1,1)},
        new PostOne(){Two = "Foo3", ThrD = new DateTime(1900,1,1)},
        new PostOne(){Two = "Bar1", ThrD = new DateTime(1900,1,1)},
        new PostOne(){Two = "Bar2", ThrD = new DateTime(1900,1,1)}
    };
    
    var authName = "Foo";
    var currentDate = new DateTime(1900,1,1);
    
    //Not sure if this is the most optimal LINQ Query, but it seems to work.
    var queryReturn = DB_PostOnes.Join(DB_PostEig.Where(x => x.EigA == authName), x => x.Two, y => y.EigTwo, (x, y) => x)
        .Where(z => z.ThrD == currentDate)
        .ToList();
    
    queryReturn.ForEach(x => Console.WriteLine(x.Two + " - " + x.ThrD)); //Foo1 - 1/1/1900
    

    编辑:

    没有连接的 LINQ 查询

    var queryTwo = DB_PostOnes
        .Where(x => DB_PostEig.Any(y => y.EigTwo == x.Two && y.EigA == authName) && x.ThrD == currentDate)
        .ToList();
    

    【讨论】:

    • 1) 为什么在 var eig 和 var data 中有多个 postone 和 posteig 实例?不就一个一个吗? 2)为什么里面有'eigtwo'和'two'?我看不出它们是如何相互关联的。他们的价值直到后来才确定......??我对你的解决方案有点迷茫,但这有点道理
    • 我无权访问您的数据库,因此我在内存数据库中创建了一个“假”来保存一些我可以查询的数据。我更新了我的命名约定,希望有助于提高可读性。
    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 2019-05-30
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多