C# 如何向LINQ查询添加列表参数?

C# 如何向LINQ查询添加列表参数?,c#,linq,list,linq-to-entities,C#,Linq,List,Linq To Entities,我将两个参数传递给该方法,需要将它们附加到最终的查询列表中 第一参数 string[] Price= new string[5]; Price= new string[] { "50", "25", "35" }; 第二参数 List<string> DiscountPrice= new List<string>(); DiscountPrice.Add ("10"); DiscountPrice.Add ("5"); DiscountPrice.Add ("3");

我将两个参数传递给该方法,需要将它们附加到最终的查询列表中

第一参数

string[] Price= new string[5];
Price= new string[] { "50", "25", "35" };
第二参数

List<string> DiscountPrice= new List<string>();
DiscountPrice.Add ("10"); 
DiscountPrice.Add ("5");
DiscountPrice.Add ("3");


var list= (from d in context.List
           where ....
           select new MyNewList
           {
                 Name = d.Name,                    
                 Country = d.Country,
                 **Price = ??** //how do I attach the parameters one by one? In the order they were saved?
                 **DiscountPrice** = ?? 

           }).ToList<MyNewList>();

听起来像是要按索引匹配列表元素。您可以从零迭代到列表元素数,并通过其索引访问每个元素:

var prices = new string[] { "50", "25", "35" };
var discountPrices = new List<string>() { "10", "5", "3" };

var items = (from d in context.List
             where ....
             select new { d.Name, d.Country }).ToList();

var list =  (from index in Enumerable.Range(0, items.Count())
             select new MyNewList
                    {
                        Name = items[index].Name,                    
                        Country = items[index].Country,
                        Price = prices[index],
                        DiscountPrice = discountPrices[index]
                    }).ToList();

听起来像是要按索引匹配列表元素。您可以从零迭代到列表元素数,并通过其索引访问每个元素:

var prices = new string[] { "50", "25", "35" };
var discountPrices = new List<string>() { "10", "5", "3" };

var items = (from d in context.List
             where ....
             select new { d.Name, d.Country }).ToList();

var list =  (from index in Enumerable.Range(0, items.Count())
             select new MyNewList
                    {
                        Name = items[index].Name,                    
                        Country = items[index].Country,
                        Price = prices[index],
                        DiscountPrice = discountPrices[index]
                    }).ToList();

您想将这些参数用作查询参数,还是将它们包含在结果中?根据您的问题,我假设您正在使用这些集合的联接?您是希望添加单个价格和折扣价格,还是希望分配集合?我只想将它们包括在结果中,将每个价格和折扣价格添加到集合中。我希望这是有意义的,THX MyNewList类中的Price和DiscountPrice的类型是什么?您希望将这些参数用作查询参数还是将它们包含在结果中?根据您的问题,我假设您正在使用这些集合的联接?您是希望添加单个价格和折扣价格,还是希望分配集合?我只想将它们包括在结果中,将每个价格和折扣价格添加到集合中。我希望这是有意义的,ThxMyNewList类中的价格和折扣价格类型是什么?