使用复杂类型实体框架ASP.NET C#构建字符串方法 [英] Build string method using complex type Entity Framework ASP.NET C#
问题描述
我不得不使用Entity Framework对我的网站应用程序进行高级搜索.
I had to use Entity Framework to equip an advanced search to my website application.
该模型是从数据库生成的,具有自己的实体,但是具有一些复杂的存储过程,该过程会返回混合列.
The model is generated from the database and has own entities but has some complex stored procedure that returning mixed columns.
为澄清起见,我有一个具有自己列的customer
实体,但我想使用从数据库导入的复杂customer_Fetch_List_Result
来实现我的高级搜索.
To clarify, I have a customer
entity with own columns but I want to use complex customer_Fetch_List_Result
that is imported from the database to implement my advanced search.
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发送到
At last I want send queryFinal to
public string Build_Customer_List(queryFinal)
生成一个包含queryFinal详细信息的html字符串,但是我找不到如何实现Build_Customer_List
函数.
to generate a html string contains queryFinal details, but I can't find how implement Build_Customer_List
function.
在此先感谢您提供任何提示和建议.
Thanks in advance for any tips and advice.
更新#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
-这是我的尝试:
But I can't call Build_Customer_List
- this was my attempt:
string post = cls.Customer_List(queryFinal.ToList<customer_Fetch_List_Result>());
或
string post = cls.Customer_List(queryFinal);
推荐答案
为此,您需要使用IQueryable
公共方法构建一个外部模型,以将数据作为IQueryable
返回,如下所示:
To do this you need to build an external model with an IQueryable
public method to returning data as IQueryable
as following:
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
查询中获取选择数据,如下所示:
Indeed, I don't know what the data-types are exactly of your data model, but we assume I guess right. know you can use the custom model customerComplex
to get select data from your linq
query as following
:
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(?).我在另一个类中定义了此函数,如下所示:
At last, you need to send the queryFinal
to a function that you mentioned it. we assume you function is Customer_List(?). I defined this function in another class as following:
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
作为以下代码发送:
Now, You can call Customer_List()
and sent the queryFinal
as folloing code:
string post = cls.Customer_List(queryFinal.AsEnumerable());
这篇关于使用复杂类型实体框架ASP.NET C#构建字符串方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!