简体   繁体   English

使用LINQ将IEnumerable转换为IList以使用不同的日期填充ListView适配器

[英]Converting IEnumerable to IList to populate ListView Adapter with Distinct Dates using LINQ

I am trying to populate my ListView adapter with unique dates.I am trying to use LINQ to pull the distinct dates from my database. 我试图用唯一的日期填充ListView适配器。我试图使用LINQ从数据库中提取不同的日期。 Note: object 'tasks' is declared as IList. 注意:对象“任务”被声明为IList。

Sample Data: 样本数据:

ID |    Date     | Task
 1    4/10/2015     A
 2    4/10/2015     B
 3    4/9/2015      B

Output: 输出:

    Date    
 4/10/2015
 4/9/2015

Here is my code: 这是我的代码:

protected override void OnResume ()
    {
        base.OnResume ();

        tasks = TaskManager.GetTasks ().GroupBy(x=> x.Created_date).Distinct();

        // create our adapter
        taskList = new Adapters.TaskListAdapter(this, tasks);

        //Hook up our adapter to our ListView
        taskListView.Adapter = taskList;
    }

public class TaskListAdapter : BaseAdapter<Task> {
        Activity context = null;
        IList<Task> tasks = new List<Task>();

        public TaskListAdapter (Activity context, IList<Task> tasks) : base ()
        {
            this.context = context;
            this.tasks = tasks;
        }

Here is my error: 这是我的错误:

Error CS0266: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<System.DateTime,Tasky.Core.Task>>' to 'System.Collections.Generic.IList<Tasky.Core.Task>'. An explicit conversion exists (are you missing a cast?) (CS0266) (TaskyAndroid)

Your task object doesn't implement IList, you need to convert it (not cast it, casting won't help, it's not wrongly casted, it's genuinely NOT something that implements ilist as it's just an IEnumerable returned by linq, not backed by an actual physical collection). 您的任务对象没有实现IList,您需要对其进行转换(不进行强制转换,强制转换将无济于事,它没有被错误地强制转换,它并不是真正实现ilist的东西,因为它只是linq返回的IEnumerable,而不是由linq支持实际的实物收藏)。

The easiest way to do this would be to turn it into a List (which does implement IList) so after your distinct, simply toss a .ToList(); 最简单的方法是将其转换为List(它确实实现了IList),因此在您独特的操作之后,只需将.ToList()扔掉即可。

tasks = TaskManager.GetTasks ().GroupBy(x=> x.Created_date).Distinct().ToList();

Make sure to change the definitiion of tasks in your class to be IList else it will still be considered an IEnumerable, or if you can't change that definition make sure you also change your call to TaskListAdapter to include the (now valid) cast. 确保将类中任务的定义更改为IList,否则仍将被视为IEnumerable,或者,如果无法更改该定义,请确保还更改对TaskListAdapter的调用以包括(现在有效)强制转换。

taskList = new Adapters.TaskListAdapter(this, (IList<Task>)tasks);

Note that this solves your casting issue but it still makes no sense because of the groupby (you have a list of groups, not of tasks) so you'll need to remove it or rework your query first to end up with just an enumeration of tasks. 请注意,这解决了强制转换问题,但是由于groupby(您有一个组列表,而不是任务列表)仍然没有意义,因此您需要先将其删除或重新处理查询才能得到一个枚举任务。

Please cast your resulting IEnumerable to IList , eg like this: 请将您产生的IEnumerableIList ,例如:

tasks = TaskManager
        .GetTasks()
        .GroupBy(x=> x.Created_date)
        .Distinct()
        .Cast<Tasky.Core.Task>()
        .ToList();

And by the way, why do you do use GroupBy() clause here? 顺便说一句,为什么在这里使用GroupBy()子句? I think .Select(x => x.Created_date).Distinct() should do. 我认为.Select(x => x.Created_date).Distinct()应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM