-1

Here i have a class:

  public class AccessinfoDetails 
{
    public IEnumerable<TurbineDvce> Devices { get; set; }
    public TixiModem TixiModem { get; set; }
}

i have an interface which i defined like this:

  public IQueryable<AccessinfoDetails> Devices(string id);

for the implemntation of the interface:

    public IQueryable<AccessinfoDetails> Devices(string id){

      AccessinfoDetails result = new AccessinfoDetails()
        {
            Devices = distinct,
            TixiModem = tixiModem
        };

        return  result;}

but i get an error saying it can not convert AcessInfoDeatils to system.linq.iquerable which is right,but how can i fix it and is it a right way to pass data to my API controller?

  • your interface defines, simply put, a _box of something_. you're returning _something_ - without the box.. – Franz Gleichmann May 15 '20 at 15:26
  • @FranzGleichmann whats the fix? –  May 15 '20 at 15:26
  • 1
    Return an `IQueryable` instead of an `AccessinfoDetails`, or change the return type to `AccessinfoDetails`. For more info, see: [What instantiate-able types implementing IQueryable are available in .Net 4.0?](https://stackoverflow.com/questions/9169915/what-instantiate-able-types-implementing-iqueryablet-are-available-in-net-4-0) – Rufus L May 15 '20 at 15:29
  • @ilmagnifico -- maybe you want to return an array with one element? – Hogan May 15 '20 at 15:31
  • @Hogan no there are other queries there but the final query is the one i assigned to result,now i want to return it but i get that error –  May 15 '20 at 15:32
  • how to return this IQueryable? i get an error –  May 15 '20 at 15:33
  • 1
    `return new List { result }.AsQueryable();` – Rufus L May 15 '20 at 15:34
  • What do you mean by *"i get an error"*? Please post the code where you're trying to return an `IQueriable`, along with the error details – Rufus L May 15 '20 at 15:36
  • Isn't it pointless to return an `IQueryable` when indeed all you have to return is a `T`? – bolkay May 15 '20 at 15:40
  • @bolkay im new in programming ,can you please tell me the right way of passing it to my controller?i dont want to do LINQ in my controller thats why i created an interface and them implemented it –  May 15 '20 at 15:41

2 Answers2

0

This should work, change your return line to look like this:

return  new [] {result}.AsQuerable();

or as Rufus L points out in the comments a typed version using list instead of array:

return new List<AccessinfoDetails> { result }.AsQueryable();
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • 'AccessinfoDetails' does not contain a definition for 'AsQuerable' and no accessible extension method 'AsQuerable' accepting a first argument of type 'AccessinfoDetails' could be found (are you missing a using directive or an assembly reference?) –  May 15 '20 at 15:38
  • did you add the using linq to the file @ilmagnifico – Hogan May 15 '20 at 15:40
  • the second approach works,but it looks ugly i dont know whats the right way of doing this really –  May 15 '20 at 15:42
  • @ilmagnifico my way works too just make sure you have the `{ }` – Hogan May 15 '20 at 15:43
0

The return type of the method is IQueryable <AccessinfoDetails>, it is a data collection, but the "result" in the method is a single data of type AccessinfoDetails.Not clear about your scenario.

If you don't want to change the return type of the method, you should change the method as shown:

 public IQueryable<AccessinfoDetails> Devices(string id)
    {
        List<AccessinfoDetails> list = new List<AccessinfoDetails>()
        {
            new AccessinfoDetails
            {
                Devices = distinct,
                ixiModem = tixiModem
            }
        };

        return list.AsQueryable();
    }
Xueli Chen
  • 11,987
  • 3
  • 25
  • 36