请查看以下链接,以防您还没有...
社交 MSDN和
论坛.Asp.Net
@Link.fr 的回答如下:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace Repository
{
class Program
{
static void Main(string[] args)
{
// get all the informations about orders
CustomersRepository oCustomersRepository = new CustomersRepository();
ProductsRepository oProductsRepository = new ProductsRepository();
OrdersRepository oOrdersRepository = new OrdersRepository();
var query1 = oOrdersRepository.SelectAll().
Join(oCustomersRepository.SelectAll(),
order => order.CustomerId,
customer => customer.Id,
(order, customer) => new
{
MyOrder = order,
MyCustomer = customer
}).
Join(oProductsRepository.SelectAll(),
item => item.MyOrder.ProductId,
product => product.Id,
(item, product) => new
{
MyOrder = item.MyOrder,
MyCustomer = item.MyCustomer,
MyProduct = product }).
ToList();
foreach (var row in query1)
{
Console.WriteLine("query1 : {0} - {1}", row.MyCustomer.Name, row.MyProduct.Name);
}
Console.WriteLine("--");
或者
var query2 = (from order in oOrdersRepository.SelectAll()
join customer in oCustomersRepository.SelectAll() on order.CustomerId equals customer.Id
join product in oProductsRepository.SelectAll() on order.ProductId equals product.Id
select
new
{
CustomerName = customer.Name,
ProductName = product.Name
}).ToList();
foreach (var row in query2)
{
Console.WriteLine("query2 : {0} - {1}", row.CustomerName, row.ProductName);
}
Console.ReadKey();
}
}
类是这样的: -
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Order
{
public int CustomerId { get; set; }
public int ProductId { get; set; }
}
public interface IRepository<T>
{
IList<T> SelectAll();
IList<T> SelectAll(Func<T, bool> expression);
}
public class CustomersRepository : IRepository<Customer>
{
public IList<Customer> SelectAll()
{
return new List<Customer>
{
new Customer{ Id = 1, Name = "Customer1"},
new Customer{ Id = 2, Name = "Customer2"},
new Customer{ Id = 3, Name = "Customer3"},
new Customer{ Id = 4, Name = "Customer4"}
};
}
public IList<Customer> SelectAll(Func<Customer, bool> expression)
{
return new List<Customer>
{
new Customer{ Id = 1, Name = "Customer1"},
new Customer{ Id = 2, Name = "Customer2"},
new Customer{ Id = 3, Name = "Customer3"},
new Customer{ Id = 4, Name = "Customer4"}
}.Where(expression).ToList();
}
}
public class ProductsRepository : IRepository<Product>
{
public IList<Product> SelectAll()
{
return new List<Product>
{
new Product{ Id = 1, Name = "Product1"},
new Product{ Id = 2, Name = "Product2"},
new Product{ Id = 3, Name = "Product3"},
new Product{ Id = 4, Name = "Product4"}
};
}
public IList<Product> SelectAll(Func<Product, bool> expression)
{
return new List<Product>
{
new Product{ Id = 1, Name = "Product1"},
new Product{ Id = 2, Name = "Product2"},
new Product{ Id = 3, Name = "Product3"},
new Product{ Id = 4, Name = "Product4"}
}.Where(expression).ToList();
}
}
public class OrdersRepository : IRepository<Order>
{
public IList<Order> SelectAll()
{
return new List<Order>
{
new Order{ CustomerId = 1, ProductId = 1},
new Order{ CustomerId = 1, ProductId = 2},
new Order{ CustomerId = 2, ProductId = 3},
new Order{ CustomerId = 3, ProductId = 4},
};
}
public IList<Order> SelectAll(Func<Order, bool> expression)
{
return new List<Order>
{
new Order{ CustomerId = 1, ProductId = 1},
new Order{ CustomerId = 1, ProductId = 2},
new Order{ CustomerId = 2, ProductId = 3},
new Order{ CustomerId = 3, ProductId = 4},
}.Where(expression).ToList();
}
}
}
可能会有所帮助。