Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic linq query not working

I am using the Dynamic Linq library found here:

http://speakingin.net/2008/01/08/dynamic-linqparte-1-usando-la-libreria-de-linq-dynamic/

http://msdn.microsoft.com/en-us/vstudio//bb894665.aspx

I have the following code in my DAL:

public IQueryable<RequestBase> GetRequestByCustomQuery(string strql)
            {
                return _context.RequestBases.Where(strql);
            }

And I have the following code in my page:

protected void BtnOpenReport_Click(object sender, EventArgs e)
        {
             var list = RequestBaseBL.GetRequestByCustomQuery("RequestNumber = 12");
            GrvResults.DataSource = list;
            GrvResults.DataBind();
        }

I am omitting all other layers, but there is a BL, then a Dal Facade and then the DL, but all they do is pass the string to the last layer, the DAL.

I get the exception

Operator '=' incompatible with operand types 'String' and 'Int32' 

The object is like this:

public class RequestBase
    {
        public int RequestBaseId { get; set; }
        public string CurrentStatus { get; set; }
        public string RequestNumber { get; set; }
        public DateTime RequestDate { get; set; }
        public bool IsOnHold { get; set; }

        public virtual Dealer Dealer { get; set; }
        public virtual Requester Requester { get; set; }
        public virtual Vehicle Vehicle { get; set; }

        public virtual ICollection<Attachment> Attachments { get; set; }
        public virtual ICollection<WorkflowHistory> WorkflowHistories { get; set; }
like image 293
Luis Valencia Avatar asked May 30 '12 12:05

Luis Valencia


Video Answer


2 Answers

That's maybe because your RequestNumber is defined as a string in the object. But in the request "RequestNumber = 12" 12 is considered as a number.

Try with "RequestNumber == \"12\""

You can't use simple quote either, because it's for character, that is to say only one letter.

like image 193
wishper Avatar answered Sep 28 '22 17:09

wishper


I think you need to change

"RequestNumber = 12"

to

"RequestNumber = '12'"

Becuase RequestNumber is a string. If you do not have the '' around it will think that it is an int

Edit

You can also try "RequestNumber == \"12\"" with escape character

like image 41
Arion Avatar answered Sep 28 '22 15:09

Arion