Where() in macro use variable or field name

Szymon Mosiolek asked on September 4, 2017 14:39

Hi,

I am building email template and want use macro with where() which will use variable or fild name. I have Order table with fieled "GarageId" and Garage table have the same filed as "DealerId" so in email templeate I want use macro which will pull garage details based on the ID. Here what I have:

  {%Documents.ClassNames("XXX.Garage").WithAllData.Where("DealerId = '94917'")[0].GetValue ("OfficeName") %}

This works fine, I am getting the right garage name so now I want use value from Order table so to display garage ID, I use:

{% Order.GarageId %}

Again I am getting right ID of garage which I choosen, so now I want combine both and have:

{% Documents.ClassNames("XXX.Garage").WithAllData.Where("DealerId = Order.GarageId")[0].GetValue ("OfficeName") %}

And this time I got nothing, so I tried with var and even converted value to sting:

{% if(Order.GarageId != null){
     id = Order.GarageId; 
     Documents.ClassNames("XXX.Garage").WithAllData.Where(DealerId=id)[0].GetValue; 
   }
%}

Still nothing and I don't have any idea how to force where to compare these two values.

Recent Answers


Chetan Sharma answered on September 4, 2017 15:39

I think the problem is missing single quotes in your second as opposed to first example. That's the difference I can see. You need to check if your query accepts OrderId with or without single quotes.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on September 5, 2017 06:57

Hi Szymon,

You should try this to combine both if you get this value correctly using macro {%Order.GarageId%}.

{% Documents.ClassNames("XXX.Garage").WithAllData.Where(string.Format("DealerId={0}", Order.GarageId))[0].GetValue("OfficeName") %}

Happy to help you!

0 votesVote for this answer Mark as a Correct answer

Szymon Mosiolek answered on September 5, 2017 11:45 (last edited on September 5, 2017 12:09)

Prashant unfortunatly this dosent work, I can't see how Where() matching GarageId with DealerId to pull data from Garages table. There are many garages so I have to match Id's to pull data from right garage.

My couleges suggesting using System.Linq library and this:

{%Documents.ClassNames("Protyre.Garage").WithAllData.Where(x => x.DealerId == Order.GarageId )[0].GetValue ("OfficeName") %}

But this dosen't work. Now I am thinking of creating custom macro and inside #c file I can use Where(), any thought if this is doable/right direction?

0 votesVote for this answer Mark as a Correct answer

Amit Srivastava answered on September 5, 2017 14:31

{%Documents.ClassNames("XXX.Garage").WithAllData.Where("DealerId = { %}{% FieldValue %}")[0].GetValue ("OfficeName") {% } %}

try above code once. it should work.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.