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

大兔子大兔子 提交于 2019-12-23 01:55:50

问题


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.

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

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.

Thanks in advance for any tips and advice.

Update #1

I tried to implement Build_Customer_List(queryFinal) as follows:

 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:

string post = cls.Customer_List(queryFinal.ToList<customer_Fetch_List_Result>());

OR

string post = cls.Customer_List(queryFinal);

回答1:


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

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

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;
}

Now, You can call Customer_List() and sent the queryFinal as folloing code:

string post = cls.Customer_List(queryFinal.AsEnumerable());



回答2:


This is your problem, read up on c# anonymous types

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



来源:https://stackoverflow.com/questions/59395284/build-string-method-using-complex-type-entity-framework-asp-net-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!