簡體   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