使用复杂类型 Entity Framework ASP.NET C# 构建字符串方法
Build string method using complex type Entity Framework ASP.NET C#
我不得不使用 Entity Framework 为我的网站应用程序配备高级搜索。
模型是从数据库生成的,有自己的实体,但有一些返回混合列的复杂存储过程。
澄清一下,我有一个带有自己列的 customer
实体,但我想使用从数据库导入的复杂 customer_Fetch_List_Result
来实现我的高级搜索。
using (DbModel db = new DbModel())
{
var query = db.customer_Fetch_List(ViewState["Gym"].ToString(), 0).AsQueryable();
if (chbFname.Checked)
{
if (!string.IsNullOrEmpty(txtcFName.Value))
{
query = query.Where(x => x.cFName.Contains(txtcFName.Value));
}
}
if (chbLname.Checked)
{
if (!string.IsNullOrEmpty(txtcLName.Value))
{
query = query.Where(x => x.cLName.Contains(txtcLName.Value));
}
}
if (chbGender.Checked)
{
if (ddlGender.Items.Count > 0)
{
query = query.Where(x => x.cSex == int.Parse(ddlGender.SelectedValue.ToString()));
}
}
if (chbStars.Checked)
{
query = query.Where(x => x.stars == rating.Value);
}
var queryFinal = query.Select(q => new
{
q.cFName,
q.cLName,
q.cSex,
q.aId,
q.cId,
q.stars
});
string output = Build_Customer_List(queryFinal); // ??
}
最后我想发送 queryFinal 到
public string Build_Customer_List(queryFinal)
生成一个包含 queryFinal 详细信息的 html 字符串,但我找不到如何实现 Build_Customer_List
函数。
提前感谢您提供任何提示和建议。
更新#1
我尝试按如下方式实现 Build_Customer_List(queryFinal)
:
public string Build_Customer_List(IQueryable<customer_Fetch_List_Result> query)
{
string post = "";
foreach (var item in query)
{
string narrow = Build_String.Get_Stars(item.stars.ToString());
string subject = item.cFName + " " + item.cLName;
post += "<a href='#' class='list-group-item'><span class='narrow'>" + narrow + "</span><i class='fa fa-fw fa-comment'></i> <span>"
+ subject + "</span></a>";
}
return post;
}
但我无法调用 Build_Customer_List
- 这是我的尝试:
string post = cls.Customer_List(queryFinal.ToList<customer_Fetch_List_Result>());
或
string post = cls.Customer_List(queryFinal);
这是你的问题,请阅读 c# 匿名类型
q => new
{
q.cFName,
q.cLName,
q.cSex,
q.aId,
q.cId,
q.stars
});
删除新的,如果需要创建一个新的类型来保存它,用
重新添加新的
new MyNewType {.. };
然后在您的方法中使用此类型
为此,您需要使用 IQueryable
public 方法构建一个外部模型,以将数据作为 IQueryable
返回,如下所示:
public class customerComplex
{
public string cFName { get; set; }
public string cLName { get; set; }
public int cSex { get; set; }
public string aId { get; set; }
public string cId { get; set; }
public int stars { get; set; }
public IQueryable<customerComplex> GetValues()
{
return new List<customerComplex>().AsQueryable();
}
}
确实,我不知道您的数据模型的数据类型到底是什么,但我们假设我猜对了。知道您可以使用自定义模型 customerComplex
从 linq
查询中获取 select 数据,如下所示
:
var queryFinal = query.Select(q => new customerComplex
{
cId = q.cId,
cFName = q.cFName,
cLName = q.cLName,
cSex = q.cSex,
aId = q.aId,
stars = q.stars
}).ToList();
最后,您需要将 queryFinal
发送到您提到的函数。我们假设你的函数是 Customer_List(?)。我在另一个 class 中定义了这个函数如下:
public string Customer_List(IEnumerable<customerComplex> query)
{
string post = "";
if(query.Count() > 0)
{
foreach (var item in query)
{
string name = item.cFName + " " + item.cLName;
post += "<p>" + name + "</p>";
}
}
return post;
}
现在,您可以调用 Customer_List()
并按以下代码发送 queryFinal
:
string post = cls.Customer_List(queryFinal.AsEnumerable());
我不得不使用 Entity Framework 为我的网站应用程序配备高级搜索。
模型是从数据库生成的,有自己的实体,但有一些返回混合列的复杂存储过程。
澄清一下,我有一个带有自己列的 customer
实体,但我想使用从数据库导入的复杂 customer_Fetch_List_Result
来实现我的高级搜索。
using (DbModel db = new DbModel())
{
var query = db.customer_Fetch_List(ViewState["Gym"].ToString(), 0).AsQueryable();
if (chbFname.Checked)
{
if (!string.IsNullOrEmpty(txtcFName.Value))
{
query = query.Where(x => x.cFName.Contains(txtcFName.Value));
}
}
if (chbLname.Checked)
{
if (!string.IsNullOrEmpty(txtcLName.Value))
{
query = query.Where(x => x.cLName.Contains(txtcLName.Value));
}
}
if (chbGender.Checked)
{
if (ddlGender.Items.Count > 0)
{
query = query.Where(x => x.cSex == int.Parse(ddlGender.SelectedValue.ToString()));
}
}
if (chbStars.Checked)
{
query = query.Where(x => x.stars == rating.Value);
}
var queryFinal = query.Select(q => new
{
q.cFName,
q.cLName,
q.cSex,
q.aId,
q.cId,
q.stars
});
string output = Build_Customer_List(queryFinal); // ??
}
最后我想发送 queryFinal 到
public string Build_Customer_List(queryFinal)
生成一个包含 queryFinal 详细信息的 html 字符串,但我找不到如何实现 Build_Customer_List
函数。
提前感谢您提供任何提示和建议。
更新#1
我尝试按如下方式实现 Build_Customer_List(queryFinal)
:
public string Build_Customer_List(IQueryable<customer_Fetch_List_Result> query)
{
string post = "";
foreach (var item in query)
{
string narrow = Build_String.Get_Stars(item.stars.ToString());
string subject = item.cFName + " " + item.cLName;
post += "<a href='#' class='list-group-item'><span class='narrow'>" + narrow + "</span><i class='fa fa-fw fa-comment'></i> <span>"
+ subject + "</span></a>";
}
return post;
}
但我无法调用 Build_Customer_List
- 这是我的尝试:
string post = cls.Customer_List(queryFinal.ToList<customer_Fetch_List_Result>());
或
string post = cls.Customer_List(queryFinal);
这是你的问题,请阅读 c# 匿名类型
q => new
{
q.cFName,
q.cLName,
q.cSex,
q.aId,
q.cId,
q.stars
});
删除新的,如果需要创建一个新的类型来保存它,用
重新添加新的new MyNewType {.. };
然后在您的方法中使用此类型
为此,您需要使用 IQueryable
public 方法构建一个外部模型,以将数据作为 IQueryable
返回,如下所示:
public class customerComplex
{
public string cFName { get; set; }
public string cLName { get; set; }
public int cSex { get; set; }
public string aId { get; set; }
public string cId { get; set; }
public int stars { get; set; }
public IQueryable<customerComplex> GetValues()
{
return new List<customerComplex>().AsQueryable();
}
}
确实,我不知道您的数据模型的数据类型到底是什么,但我们假设我猜对了。知道您可以使用自定义模型 customerComplex
从 linq
查询中获取 select 数据,如下所示
:
var queryFinal = query.Select(q => new customerComplex
{
cId = q.cId,
cFName = q.cFName,
cLName = q.cLName,
cSex = q.cSex,
aId = q.aId,
stars = q.stars
}).ToList();
最后,您需要将 queryFinal
发送到您提到的函数。我们假设你的函数是 Customer_List(?)。我在另一个 class 中定义了这个函数如下:
public string Customer_List(IEnumerable<customerComplex> query)
{
string post = "";
if(query.Count() > 0)
{
foreach (var item in query)
{
string name = item.cFName + " " + item.cLName;
post += "<p>" + name + "</p>";
}
}
return post;
}
现在,您可以调用 Customer_List()
并按以下代码发送 queryFinal
:
string post = cls.Customer_List(queryFinal.AsEnumerable());