1
Iam在Linq-to-Sql上試圖獲取傳遞給動態obj參數的列名(每次更改)。我需要選擇定義動態OBJ數組的列名。Linq to Sql-動態列選擇
PS:我需要選擇SKU,VENDORNAME,vendorstylecode一次
「where」子句條件,工作正常,但我需要選擇定義data.col指數
我試過以下特定的列代碼,但不幫助我:
public HttpResponseMessage PostGenerateFile([FromBody] dynamic data) {
string[] vendorname = data.vendorname != null ?data.vendorname.ToObject<string[]>() : null;
string[] brandname = data.brandname != null ? data.brandname.ToObject<string[]>() : null;
using (var context = new Vendor_InvDataContext())
{ var query = context.AllInventories.AsQueryable();
for (int i = 0; i < data.col.Count; i++)
{
if(data.col[i]=="SKU")
query.Select(s => s.SKU);
if (data.col[i] == "VENDORNAME")
query.Select(s => s.VENDORNAME);
if (data.col[i] == "VENDORSTYLECODE")
query.Select(s => s.VENDORSTYLECODE);
if (data.col[i] == "STYLECODE")
query.Select(s => s.STYLECODE);
if (data.col[i] == "STYLENAME")
query.Select(s => s.STYLENAME);
}
if (vendorname != null && vendorname.Length > 0)
{
query = query.Where(s => vendorname.Contains(s.VENDORNAME));
}
if (brandname != null && brandname.Length > 0)
{
query = query.Where(s => brandname.Contains(s.BRANDNAME));
}
var items = query.ToList();