Look-up Selected Attribute Field Error

516
5
02-21-2022 07:55 PM
ThiPham12
New Contributor III

I am trying to return a list of selected feature attribute field, "mukey" from a feature class, "MUPOLYGON". I tried to look-up the mukey using the GetTable() method and creating a mukey list. However, I am getting an error that says cannot convert a Task to a string parameter when I tried to use the mukey list as a parameter for another method. I have the following questions: 

1. What is a light weight method to obtain a mukey list from the selected feature field, mukey? 

2. How do I resolve the conversion error? 

public async Task LookupMukey()
{
var mukeys = new List<string>();
var MV = MapView.Active;
int mukeyCount = 0;
var SelectionLayer = MV.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains("SelectionLayer")).FirstOrDefault();

await QueuedTask.Run(() =>
{
using (var SelectionTable = SelectionLayer.GetTable())
{
using (var rowCursor = SelectionTable.Search())
{
while (rowCursor.MoveNext())
{
using (Row row = rowCursor.Current)
{
var mukey = Convert.ToString(row["mukey"]);
mukeys.Add(mukey);
mukeyCount++;
}
}
}
}
return mukeys;
});
}

Using the mukey list as a parameter for another method: 

Process.ChorizonData(mukeys,"awc_l", "awc_r", "awc_h", inputPath);  //error occurs here at mukey

Thanks for your help!

0 Kudos
5 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Your function must be like this :

        public async Task<List<string>> LookupMukey()
        {
            var mukeys = new List<string>();
            var MV = MapView.Active;
            int mukeyCount = 0;
            var SelectionLayer = MV.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains("SelectionLayer")).FirstOrDefault();

            await QueuedTask.Run(() =>
            {
                using (var SelectionTable = SelectionLayer.GetTable())
                {
                    using (var rowCursor = SelectionTable.Search())
                    {
                        while (rowCursor.MoveNext())
                        {
                            using (Row row = rowCursor.Current)
                            {
                                var mukey = Convert.ToString(row["mukey"]);
                                mukeys.Add(mukey);
                                mukeyCount++;
                            }
                        }
                    }
                }
            });
            return mukeys;
        }

And call to function:

var mukeys = await LookupMukey();
0 Kudos
ThiPham12
New Contributor III

Thanks for your help Gintautas! When I tried to call the function, I am getting an error that says, "CS4034: The await operator can only be used with an async lambda expression. Consider marking this lambda expression with the async modifier." Where do I add the async modifier to the lambda expression? 

Is this right? await QueuedTask.Run(async() =>...  //error: lack await operator

 

0 Kudos
GKmieliauskas
Esri Regular Contributor

You can add "async" to "var mukeys = await LookupMukey();" calling method like this:

public async voif Your_Method()
{
    var mukeys = await LookupMukey();
}

or change LookupMukey method calling to:

var mukeys = LookupMukey().Result;
0 Kudos
tzz_12
by
New Contributor III

Hi Gintautas, 

What if I am calling the LookupMukey method in a ICommand event handler? How should I call the LookupMukey()? Please see the example below: 

//within a public ICommand CmdRun binded to a Run button

AllProcesses Process = new AllProcesses();

var mukeys = await Process.LookupMukey();    //error occurs here

//codes below - calling multiple methods and passing the list of mukeys as one of the parameters

Thanks for your help!

 

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

If I understand right, it must be like this:

        protected XViewModel()
        {
            _CommandX = new RelayCommand(() => CmdX(), true);
        }

        private ICommand _CommandX;
        public ICommand CommandX
        {
            get { return _CommandX; }
        }

        /// <summary>
        /// Execute the X command
        /// </summary>
        private async void CmdX()
        {
		AllProcesses Process = new AllProcesses();
		var mukeys = await Process.LookupMukey();
		//codes below - calling multiple methods and passing the list of mukeys as one of the parameters
        }

	// or

        private void CmdX()
        {
		AllProcesses Process = new AllProcesses();
		var mukeys = Process.LookupMukey().Result;
		//codes below - calling multiple methods and passing the list of mukeys as one of the parameters
        }
0 Kudos