3

This is a SL/WPF application, trying to display two columns. The following code:

MyDomainContext context = new MyDomainContext();
dataGrid1.ItemsSource = context.DBTables;
context.Load(context.GetTwoDataBasesQuery())

;

The domainservice.cs contains the method definition as follows:

public IQueryable<DBTable>GetTwoDataBases()
  {
    return this.ObjectContext.DBTables;
  }

This code works fine but returns all columns in the context I need to return only two columns so changed is as follows

public IQueryable<DBTable>GetTwoDataBases()
  {
      //trying to return columns
      return GetDBTables().Select(m => new { m.col1, m.col2 });                           
  }

But compiler generates errors, does not accept the “return”.

following error Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists.

How to return two columns only? many thanX

1 Answer 1

1

You are returning the anonymous type but the return type of collection you have is DBTable. You can make the return type object or define a new class and create object of that class.

Making object as return type

public object GetTwoDataBases()
{
     //trying to retrun columns
     return GetDBTables().Select(m => new { m.col1, m.col2 });    
}

OR, returning IQueryable instead of anonymous type

public IQueryable<YourCustomType>GetTwoDataBases()
{
     //trying to retrun columns
     return GetDBTables()
            .Select(m => new YourCustomType { YourCustomTypeProperty1 = m.col1, YourCustomTypeProperty2 = m.col2 });    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.