简体   繁体   English

无法在C#中找出IQueryable

[英]Can't figure out IQueryable in C #

This is my class: 这是我的课:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
{
    public class Attendee
    {
        public int AttendeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Later in the code I am trying to use that class but it is declared as IEnumerable : 稍后在代码中,我尝试使用该类,但将其声明为IEnumerable

IQueryable<Attendee> myList;

I need to able to manually populate it. 我需要能够手动填充它。 Something like this: 像这样:

IQueryable<Attendee> districtList = new IQueryable<Attendee>() { AtendeeId = 1, FirstName = "First Name", LastName = "Last Name" }; <- This does not work. I need example of how to add one item or more then one item.

I would really appreciate an example of how to populate it with some fake data manually like "Test String", "test ID" etc. myList is just a variable. 我真的很喜欢一个示例,该示例说明如何使用一些虚假数据手动填充它,例如“测试字符串”,“测试ID”等。myList只是一个变量。

To fix it I did the following: 为了解决这个问题,我做了以下工作:

var attendees = new List<Attendee>();
// Manually Populate Attendees list

myList = attendees.AsQueriable();

IQueryable<T> is an interface, so you can't instantiate it directly. IQueryable<T>是一个接口,因此无法直接实例化它。 Instead, you need to create an instance of a class that implements the interface. 相反,您需要创建一个实现该接口的类的实例。 For example: 例如:

List<string> myList = new List<string>(){"foo", "bar"};
IQueryable<string> myQueryable = myList.AsQueryable();

Note: In this example, I'm using the LINQ AsQueryable() method to convert from an IEnumerable<T> (ie List<T> ) to an IQueryable<T> . 注意:在此示例中,我使用LINQ AsQueryable()方法将IEnumerable<T> (即List<T> )转换为IQueryable<T>

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

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