[英]Build string method using complex type Entity Framework ASP.NET C#
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.该模型是从数据库生成的,具有自己的实体,但具有一些返回混合列的复杂存储过程。
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.澄清一下,我有一个带有自己列的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); // ??
}
At last I want send queryFinal to最后我想发送 queryFinal 到
public string Build_Customer_List(queryFinal)
to generate a html string contains queryFinal details, but I can't find how implement Build_Customer_List
function.生成包含 queryFinal 详细信息的 html 字符串,但我找不到如何实现Build_Customer_List
函数。
Thanks in advance for any tips and advice.在此先感谢您的任何提示和建议。
Update #1更新 #1
I tried to implement Build_Customer_List(queryFinal)
as follows:我尝试实现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;
}
But I can't call Build_Customer_List
- this was my attempt:但我不能调用Build_Customer_List
- 这是我的尝试:
string post = cls.Customer_List(queryFinal.ToList<customer_Fetch_List_Result>());
OR或者
string post = cls.Customer_List(queryFinal);
This is your problem, read up on c# anonymous types这是你的问题,阅读 c# 匿名类型
q => new
{
q.cFName,
q.cLName,
q.cSex,
q.aId,
q.cId,
q.stars
});
Remove new, and if needed create a new type to hold this, adding back in new with删除新的,如果需要创建一个新的类型来保存它,添加回新的
new MyNewType {.. };
THen use this type in your method然后在你的方法中使用这种类型
To do this you need to build an external model with an IQueryable
public method to returning data as IQueryable
as following:为此,您需要使用IQueryable
公共方法构建外部模型,以将数据作为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();
}
}
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知道您可以使用自定义模型customerComplex
从您的linq
查询中获取选择数据,如下所示
: :
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();
At last, you need to send the queryFinal
to a function that you mentioned it.最后,您需要将queryFinal
发送到您提到的函数。 we assume you function is Customer_List(?).我们假设您的函数是 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;
}
Now, You can call Customer_List()
and sent the queryFinal
as folloing code:现在,您可以调用Customer_List()
并将queryFinal
作为以下代码发送:
string post = cls.Customer_List(queryFinal.AsEnumerable());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.