我正在尝试联接两个表,即基于产品表和商店表 我的加入条件是产品ID,在这里我的查询总是返回0作为值
List<Store> list1 = new List<Store>();
List<Product> list2 = new List<Product>();
var query = list1.Join(
list2,
st => st.ProductId,
Pd => Pd.productId,
(st, Pd) => new
{
StoreName = st.storeName,
StoreId = st.storeName,
productId = st.ProductId,
producRate = Pd.productRating
}
).ToList();
答案 0 :(得分:0)
您的数据或键的匹配方式似乎有问题:
internal class Program
{
private static void Main(string[] args)
{
var products = Enumerable
.Range(1, 10)
.Select(i => new Product { ProductId = i, ProductRating = i / 2d })
.ToList();
var stores = Enumerable
.Range(1, 10)
.Select(i => new Store { ProductId = i + 5, StoreId = i + 100, StoreName = $"My Store {i}" })
.ToList();
var query = stores.Join(
products,
store => store.ProductId,
product => product.ProductId,
(store, product) => new
{
store.StoreName,
store.StoreId,
product.ProductId,
product.ProductRating,
});
foreach (var item in query)
{
Console.WriteLine($"{item.ProductId} - {item.StoreId} - {item.StoreName} - {item.ProductRating}");
}
Console.WriteLine("Finished");
Console.ReadKey();
}
}
public class Product
{
public int ProductId { get; set; }
public double ProductRating { get; set; }
}
public class Store
{
public int ProductId { get; set; }
public int StoreId { get; set; }
public string StoreName { get; set; }
}
输出:
6 - 101 - My Store 1 - 3
7 - 102 - My Store 2 - 3,5
8 - 103 - My Store 3 - 4
9 - 104 - My Store 4 - 4,5
10 - 105 - My Store 5 - 5
Finished