-1

I have SQL text query. I not want to use linq, it will be complicated linq.

How to get IQueryable expression from my SQL string, which i can use in other linq queries? I want to get something like this:

public class ElementCount
{
   public int ElementID { get; set; }
   public int ElementCount { get; set; }
}

string sql = "<element count query>";

IQueryable<ElementCount> elementsCount = GetExpression(sql);

dbContext.Elements.Join(elementsCount ,element=>element.ID,elementCount=>elementCount.ElementID,

... etc or something other linq query

michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
Denis Fedak
  • 161
  • 12
  • 1
    I think [`SqlQuery`](https://msdn.microsoft.com/en-us/library/system.data.entity.dbset.sqlquery%28v=vs.113%29.aspx) might serve your purpose. I don't know if it is possible to get `IQueryable` from raw sql, but `SqlQuery` doesn't execute your query until enumerated. If what are you looking for is being able to call LINQ methods, you will be able to, because they have also `IEnumerable` overrides. – Paweł Hemperek Oct 13 '16 at 07:08
  • I tried. This does not work. SqlQuery return DbRawSqlQuery – Denis Fedak Oct 13 '16 at 07:10
  • Why exactly do you need `IQueryable`? – Paweł Hemperek Oct 13 '16 at 07:11
  • It's not exactly. I want to get expression which i can use in other linq to sql queries – Denis Fedak Oct 13 '16 at 07:13
  • Well, [I don't think it's possible if you're not using EF7](http://stackoverflow.com/questions/32144330/iqueryablet-from-raw-sql) – Paweł Hemperek Oct 13 '16 at 07:18

1 Answers1

1

Does your query have to be defined in C#?

Because if not, you can always make it into a View or Stored Procedure in the DB and map that into your DbContext as an IQueryable.

H. Lowette
  • 314
  • 1
  • 9