繁体   English   中英

使用复杂类型 Entity Framework ASP.NET C# 构建字符串方法

[英]Build string method using complex type Entity Framework ASP.NET C#

我不得不使用实体框架为我的网站应用程序配备高级搜索。

该模型是从数据库生成的,具有自己的实体,但具有一些返回混合列的复杂存储过程。

澄清一下,我有一个带有自己列的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公共方法构建外部模型,以将数据作为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查询中获取选择数据,如下所示

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(?)。 我在另一个类中定义了这个函数,如下所示:

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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM