Linq两张桌子中的一张

时间:2012-12-27 16:37:47

标签: c# .net linq entity-framework

我有两张桌子:

  1. Id,FirtsName,LastName,Adress,Date
  2. ID,FirstName,LastName,电子邮件,电话,日期
  3. 现在我想收到结果:

    • Id,FirtsName,LastName,Adress,Date
    • ID,FirstName,LastName,电子邮件,电话,日期
    • Id,FirtsName,LastName,Adress,Date
    • ID,FirstName,LastName,电子邮件,电话,日期
    • Id,FirtsName,LastName,Adress,Date
    • ID,FirstName,LastName,电子邮件,电话,日期

    如何使用Linq做到这一点?

    好的我有更多的解释,这是我的任务:两者之间必须存在关系,以便同时从两个表中收集信息以创建预先填充的PDF。我必须使用Linq和MVC 4.5

    我认为这会很容易,但圣诞节后我会有一些心智盲点。

    这就是我想要实现的目标:

    var last24h = DateTime.Now.AddHours(-24);
            var resultL = db.Loans.Where(p => p.CreatedDate >= last24h).Select( p=> new PDFList() {
                Id = p.Id,
                Amount = p.Amount,
                Term = p.Term,
                Purpose = p.Purpose,
                FirstName = p.FirstName,
                LastName = p.LastName,
                Address = p.Address,
                Email = p.Email,
                PropertyValue = 0,
                CreatedDate = p.CreatedDate,
                Seen = p.Seen,
                AdvisorId = p.AdvisorId
            });
            var resultM = db.Mortgages.Where(p => p.CreatedDate >= last24h).Select(p => new PDFList()
            {
                Id = p.Id,
                Amount = p.Amount,
                Term = p.Term,
                Purpose = "",
                FirstName = p.FirstName,
                LastName = p.LastName,
                Address = p.Address,
                Email = p.Email,
                PropertyValue = p.PropertyValue,
                CreatedDate = p.CreatedDate,
                Seen = p.Seen,
                AdvisorId = p.AdvisorId
            });
            var resultLM = resultL.Concat(resultM).OrderByDescending(p=>p.CreatedDate).ToList();
    

1 个答案:

答案 0 :(得分:1)

您可以按照下面提到的类型内连接为您的表。尝试。

这是Linq内部联接的示例

public void Linq102() 
{ 

    string[] categories = new string[]{  
        "Beverages",   
        "Condiments",   
        "Vegetables",   
        "Dairy Products",   
        "Seafood" };  

    List<Product> products = GetProductList(); 

    var q = 
        from c in categories 
        join p in products on c equals p.Category 
        select new { Category = c, p.ProductName }; 

    foreach (var v in q) 
    { 
        Console.WriteLine(v.ProductName + ": " + v.Category);  
    } 
}

您可以获得更多详细信息LINQ - Join Operators Here

我希望这会对你有所帮助。