C# - 在 Linq Where Any 语句中使用的要列出的字符串
C# - String to list used in Linq Where Any statement
我想使用这个字符串作为过滤器来删除 linq 查询中的一些 ID
public class ProductKitMakerDto
{
public int Id { get; set; }
public string TitleShort { get; set; }
public string Media { get; set; }
}
[HttpPost]
public ActionResult KitItemSelect(string culture)
{
string productMakerIds = "4174,2196,2201,2460,2508,2204";
//create a list
var productMakerList = new List<ProductKitMakerDto>();
foreach (int i in productMakerIds)
{
productMakerList.Add(new ProductKitMakerDto { Id = i });
}
var itemselects = (from p in _context.Products
where p.Matrix == 2400
select new ProductKitMakerDto()
{
Id = p.Id,
TitleShort = culture == "de" ? p.TitleShortDe :
culture == "fr" ? p.TitleShortFr :
p.TitleShortEn,
Media = "/img/" + p.Photo,
}).ToList();
//From this query I get 40 results.
//Then I want to remove the ones from the list:
//itemselects = itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
//1st (above) I get an Error CS0266 asking for explicit cast. So aplly the modification
itemselects = (List<ProductKitMakerDto>)itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
return Json(itemselects, JsonRequestBehavior.AllowGet);
}
我收到 500(内部服务器错误)- xhr.send( options.hasContent && options.data || null );
我猜列表是空的。
任何想法?谢谢
foreach (string i in productMakerIds.Split(','))
{
productMakerList.Add(new ProductKitMakerDto { Id = int.Parse(i) });
}
这行不通
string productMakerIds = "4174,2196,2201,2460,2508,2204";
var productMakerList = new List<ProductKitMakerDto>();
foreach (int i in productMakerIds)
{
productMakerList.Add(new ProductKitMakerDto { Id = i });
}
因为你需要先用逗号分割然后将 string
解析为 int
:
foreach (string i in productMakerIds.Split(',')) // and parse i to int with int.Parse
但是因为它是一个字符串文字,所以首先要正确地初始化它。不要使用 List<ProductKitMakerDto>
因为你只需要一个 List<int>
,那么你可以使用 Contains
:
var productMakerList = new List<int>
{
4174, 2196, 2201, 2460, 2508 , 2204
};
如果它不是列表并且 Enumerable.Where
不是 return 一个:
,则不能转换为列表
itemselects = (List<ProductKitMakerDto>)itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
您需要在 Where
之后附加 ToList
itemselects = itemselects
.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id))
.ToList();
但如前所述,您也可以在第一次创建该列表之前使用此 Where
,因此请在 Contains
中包含应支持的条件:
var itemselects = (from p in _context.Products
where p.Matrix == 2400
&& !productMakerList.Contains(p.Id)
select new ProductKitMakerDto()
{
Id = p.Id,
TitleShort = culture == "de"
? p.TitleShortDe
: culture == "fr" ? p.TitleShortFr : p.TitleShortEn,
Media = "/img/" + p.Photo,
}).ToList();
我想使用这个字符串作为过滤器来删除 linq 查询中的一些 ID
public class ProductKitMakerDto
{
public int Id { get; set; }
public string TitleShort { get; set; }
public string Media { get; set; }
}
[HttpPost]
public ActionResult KitItemSelect(string culture)
{
string productMakerIds = "4174,2196,2201,2460,2508,2204";
//create a list
var productMakerList = new List<ProductKitMakerDto>();
foreach (int i in productMakerIds)
{
productMakerList.Add(new ProductKitMakerDto { Id = i });
}
var itemselects = (from p in _context.Products
where p.Matrix == 2400
select new ProductKitMakerDto()
{
Id = p.Id,
TitleShort = culture == "de" ? p.TitleShortDe :
culture == "fr" ? p.TitleShortFr :
p.TitleShortEn,
Media = "/img/" + p.Photo,
}).ToList();
//From this query I get 40 results.
//Then I want to remove the ones from the list:
//itemselects = itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
//1st (above) I get an Error CS0266 asking for explicit cast. So aplly the modification
itemselects = (List<ProductKitMakerDto>)itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
return Json(itemselects, JsonRequestBehavior.AllowGet);
}
我收到 500(内部服务器错误)- xhr.send( options.hasContent && options.data || null ); 我猜列表是空的。 任何想法?谢谢
foreach (string i in productMakerIds.Split(','))
{
productMakerList.Add(new ProductKitMakerDto { Id = int.Parse(i) });
}
这行不通
string productMakerIds = "4174,2196,2201,2460,2508,2204"; var productMakerList = new List<ProductKitMakerDto>(); foreach (int i in productMakerIds) { productMakerList.Add(new ProductKitMakerDto { Id = i }); }
因为你需要先用逗号分割然后将 string
解析为 int
:
foreach (string i in productMakerIds.Split(',')) // and parse i to int with int.Parse
但是因为它是一个字符串文字,所以首先要正确地初始化它。不要使用
List<ProductKitMakerDto>
因为你只需要一个List<int>
,那么你可以使用Contains
:var productMakerList = new List<int> { 4174, 2196, 2201, 2460, 2508 , 2204 };
如果它不是列表并且
,则不能转换为列表Enumerable.Where
不是 return 一个:itemselects = (List<ProductKitMakerDto>)itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
您需要在 Where
ToList
itemselects = itemselects
.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id))
.ToList();
但如前所述,您也可以在第一次创建该列表之前使用此 Where
,因此请在 Contains
中包含应支持的条件:
var itemselects = (from p in _context.Products
where p.Matrix == 2400
&& !productMakerList.Contains(p.Id)
select new ProductKitMakerDto()
{
Id = p.Id,
TitleShort = culture == "de"
? p.TitleShortDe
: culture == "fr" ? p.TitleShortFr : p.TitleShortEn,
Media = "/img/" + p.Photo,
}).ToList();