[英]how to use string variable as column name in asp.net mvc
I am not sure that such a thing is possible. 我不确定这种事情是否可能。 But I want to use a variable as column name. 但是我想使用变量作为列名。
Below is the code I have to use 以下是我必须使用的代码
cartall.CartItems = cartdatas.Select(a => new Models.DTO.CartDTO.CartVM()
{
VariationId = a.VariationId,
ColorName = a.ColorName,
StockInfo = rpstock.FirstOrDefault(x => x.Id == a.VariationId).Yellow
}).ToList();
But I want to use it like below. 但是我想像下面那样使用它。
The code I want to use: 我要使用的代码:
cartall.CartItems = cartdatas.Select(a => new Models.DTO.CartDTO.CartVM()
{
VariationId = a.VariationId,
ColorName = a.ColorName,
StockInfo = rpstock.FirstOrDefault(x => x.Id == a.VariationId).(a.ColorName)
}).ToList();
My Stock.cs 我的Stock.cs
public class Stock:Base.BaseEntity
{
public int Id { get; set; }
public int? Yellow { get; set; }
public int? Red { get; set; }
public int? White { get; set; }
public virtual VariationEntity.Variation Variation { get; set; }
}
My Variation.cs 我的变体
public class Variation : Base.BaseEntity
{
public int Id { get; set; }
public int OwnerProductID { get; set; }
public string SKU { get; set; }
public short? Height { get; set; }
public short? Width { get; set; }
public decimal? Price { get; set; }
public string Delivery { get; set; }
public int? OrderLimit { get; set; }
public virtual StockEntity.Stock Stock { get; set; }
}
I have 1 to 1 relationship between stock.cs and variation.cs 我在stock.cs和variant.cs之间有1对1的关系
这应该工作:
StockInfo = rpstock.FirstOrDefault(x => x.Id == a.VariationId && x.ColorName == a.ColorName)
I've created a Method
which will take a list
and a Column Name
then return value
of the column. 我创建了一个Method
,它将使用一个list
和一个Column Name
然后返回该列的value
。 Please check this. 请检查一下。
Code: 码:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Stock
{
public int Id { get; set; }
public int? Yellow { get; set; }
public int? Red { get; set; }
public int? White { get; set; }
}
public static void Main()
{
List<Stock> list = new List<Stock>();
list.Add(new Stock(){Id=1, Yellow = 50, Red = 0, White = 205});
list.Add(new Stock(){Id=2, Yellow = 20, Red = 200, White = 35});
list.Add(new Stock(){Id=3, Yellow = 0, Red = 100, White = 155});
string ColumnName = "Yellow";
var Test1 = GetColumnValue(list.Where(m=>m.Id == 1).ToList(), ColumnName);
var Test2 = GetColumnValue(list.Where(m=>m.Id == 2).ToList(), ColumnName);
var Test3 = GetColumnValue(list.Where(m=>m.Id == 3).ToList(), ColumnName);
Console.WriteLine(Test1);
Console.WriteLine(Test2);
Console.WriteLine(Test3);
}
public static object GetColumnValue(List<Stock> items, string columnName)
{
var values = items.Select(x => x.GetType().GetProperty(columnName).GetValue(x)).FirstOrDefault();
return values;
}
}
You have to use the method GetColumnValue
in your code like this: 您必须像下面这样在代码中使用GetColumnValue
方法:
cartall.CartItems = cartdatas.Select(a => new Models.DTO.CartDTO.CartVM()
{
VariationId = a.VariationId,
ColorName = a.ColorName,
StockInfo = GetColumnValue(rpstock.FirstOrDefault(x => x.Id == a.VariationId).ToList(), a.ColorName)
}).ToList();
You can run this solution in DotNetFiddle . 您可以在DotNetFiddle中运行此解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.