locked
Help Solving: Does not contain a definition for 'Select'..... RRS feed

  • Question

  • Hi,

    Need help solving a Task that returns a Task<IEnumerable<Writing>> so I can fill ObservableCollection<ViewModels.IWritingItemViewModel> Writings for my Design Time Data Page:

    <d:Page.DataContext>
    	<designTimeData:MainPageViewModel />
    </d:Page.DataContext>

    My constructor does this:

    public MainPageViewModel()
    {
    	var writings = this.GetGroupsAsync();
    	this.Writings = new ObservableCollection<ViewModels.IWritingItemViewModel>();
    
    	var viewmodels = writings.Select((x, i) => new WritingItemViewModel
    	{
    		Writing = x,
    		VariableItemSize = (i == 0) ? Common.VariableItemSizes.Writings : Common.VariableItemSizes.Normal,
    	});
    }


    My var writings = this.GetGroupsAsync(); is:

    public async Task<IEnumerable<Writing>> GetGroupsAsync()
    {
    	await this.GetMenuDataAsync();
    
    	return this.Groups;
    }


    which in turn gets data from:

    private async Task GetMenuDataAsync()
    {
    	Uri dataUri = new Uri("ms-appx:///DesignTimeData/MenuData.json");
    
    	StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
    	string jsonText = await FileIO.ReadTextAsync(file);
    	JsonObject jsonObject = JsonObject.Parse(jsonText);
    	JsonArray jsonArray = jsonObject["Groups"].GetArray();
    
    	foreach (JsonValue groupValue in jsonArray)
    	{
    		JsonObject groupObject = groupValue.GetObject();
    		Writing group = new Writing(
    			groupObject["UniqueId"].GetString(),
    			groupObject["IsHeaderInteractive"].GetBoolean(),
    			groupObject["ViewType"].GetString(),
    			groupObject["ModelType"].GetString(),
    			groupObject["Page"].GetString(),
    			groupObject["Title"].GetString(),
    			groupObject["Subtitle"].GetString(),
    			groupObject["ImagePath"].GetString(),
    			groupObject["Description"].GetString(),
    			groupObject["GroupId"].GetString()
    			);
    
    		foreach (JsonValue itemValue in groupObject["WritingMenus"].GetArray())
    		{
    			JsonObject itemObject = itemValue.GetObject();
    			group.WritingMenus.Add(new WritingMenu(
    				itemObject["UniqueId"].GetString(),
    				itemObject["Page"].GetString(),
    				itemObject["Title"].GetString(),
    				itemObject["Subtitle"].GetString(),
    				itemObject["ImagePath"].GetString(),
    				itemObject["Description"].GetString(),
    				itemObject["Content"].GetString(),
    				itemObject["WritingsId"].GetString(),
    				itemObject["GroupId"].GetString(),
    				Convert.ToInt32(itemObject["Item"].ValueType)
    				));
    		}
    		this.Groups.Add(group);
    	}
    }


    and I get this Error:

    Error 5 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<App1.DesignTimeData.Writing>>' 
      does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 
      'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<App1.DesignTimeData.Writing>>' 
    could be found (are you missing a using directive or an assembly reference?)


    I'm creating collections of:

    public interface IWritingItemViewModel : Common.IVariableSizedItem
    {
    	Models.Writing Writing { get; set; }
    }

    and my class for creating data is:

    public class Writing
    {
    	public Writing(
    	string uniqueId,
    	bool isHeaderInteractive,
    	string templateType,
    	string viewModelType,
    	string page,
    	string title,
    	string subtitle,
    	string imagePath,
    	string description,
    	string groupId
    		)
    	{
    		this.UniqueId = uniqueId;
    		this.IsHeaderInteractive = isHeaderInteractive;
    		this.TemplateType = templateType;
    		this.ViewModelType = viewModelType;
    		this.Page = page;
    		this.Title = title;
    		this.Subtitle = subtitle;
    		this.ImagePath = imagePath;
    		this.Description = description;
    		this.GroupId = groupId;
    		this.WritingMenus = new ObservableCollection<WritingMenu>();
    	}
    
    	public string UniqueId { get; private set; }
    	public bool IsHeaderInteractive { get; private set; }
    	public string TemplateType { get; private set; }
    	public string ViewModelType { get; private set; }
    	public string Page { get; private set; }
    	public string Title { get; private set; }
    	public string Subtitle { get; private set; }
    	public string ImagePath { get; private set; }
    	public string Description { get; private set; }
    	public string GroupId { get; private set; }
    
    	public virtual ObservableCollection<WritingMenu> WritingMenus { get; private set; }
    }
    

    How can I solve or successfully complete this code?

    Thanks!...


    Code is like a box of chocolates!...




    • Edited by VcDeveloper Tuesday, February 24, 2015 6:38 AM more info
    Tuesday, February 24, 2015 6:32 AM

Answers

  • I looked into your error message again, I found something interesting:

    Error 5 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<App1.DesignTimeData.Writing>>'
      does
    not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type
     
    'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<App1.DesignTimeData.Writing>>'
      could be found (are you missing a using directive or an assembly reference?)

    That means: Task does not contain the definition for Select. This was reasonable, Task does not inherit IEnumerable interface.

    	var viewmodels = writings.Select((x, i) => new WritingItemViewModel
    	{
    		Writing = x,
    		VariableItemSize = (i == 0) ? Common.VariableItemSizes.Writings : Common.VariableItemSizes.Normal,
    	});
    
            public async Task<IEnumerable<Writing>> GetGroupsAsync()

    You need get the IEnumerable<Writing> for Select use. See this for more information: How to: Return a Value from a Task

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 2, 2015 8:12 AM
    Moderator

All replies

  • Hi VcDeveloper,

    The first error looks like the Linq you are using is not correct.

    Did you have referenced to "using System.Linq;" in your code?

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 25, 2015 3:29 AM
    Moderator
  • Hi James,

    Yes I have and for now I had to hand code the sample data which; I wanted to avoid by trying to read a .json file, but I couldn't get the async Task to work.


    Code is like a box of chocolates!...

    Thursday, February 26, 2015 5:20 AM
  • I looked into your error message again, I found something interesting:

    Error 5 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<App1.DesignTimeData.Writing>>'
      does
    not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type
     
    'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<App1.DesignTimeData.Writing>>'
      could be found (are you missing a using directive or an assembly reference?)

    That means: Task does not contain the definition for Select. This was reasonable, Task does not inherit IEnumerable interface.

    	var viewmodels = writings.Select((x, i) => new WritingItemViewModel
    	{
    		Writing = x,
    		VariableItemSize = (i == 0) ? Common.VariableItemSizes.Writings : Common.VariableItemSizes.Normal,
    	});
    
            public async Task<IEnumerable<Writing>> GetGroupsAsync()

    You need get the IEnumerable<Writing> for Select use. See this for more information: How to: Return a Value from a Task

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 2, 2015 8:12 AM
    Moderator