0

I've created a SQL query that I execute with the following command, and it returns the correct number of entries but these contains all 0:

enter image description here

If I run the same SQL command in my Management Studio, it works correctly.

I also tried it with a Linq statement and it works also correctly:

enter image description here

I hope you guys can help me to solve the problem.

5
  • 1
    I would start by using parameterized SQL instead of including filter directly into the query string. That may or may not be all that's required - it's hard to tell with relatively little schema or type information. Commented Apr 12, 2018 at 7:53
  • 2
    Please don't use images, post code instead. Commented Apr 12, 2018 at 7:54
  • Please read the accepted answer of "Why not upload images of code on SO when asking a question?" and edit your question accordingly. Commented Apr 12, 2018 at 7:55
  • Can you show an expanded view of the data returned in the straight sql query, count=0 there must be something in it or it wouldnt be returned.. Commented Apr 12, 2018 at 7:55
  • The images is not only hard to read, but also imgur is blocked from my workplace, so I can't even see them. Commented Apr 12, 2018 at 7:58

1 Answer 1

3

You shouldn't be projecting into a List<T> - the ToList() does that. Basically, simplify:

var data = dbContext.Database.SqlQuery<Tuple<DateTime, string, string>>(...).ToList();

It might also work with value-tuples:

var data = dbContext.Database.SqlQuery<(DateTime, string, string)>(...).ToList();

which would also allow you to conceptually name them:

var data = dbContext.Database.SqlQuery<(DateTime Datum, string Text, string Bemerkung)>
       (...).ToList();

Note: concatenating filter is almost certainly a SQL injection vulnerability; if looks like you should be using a SQL parameter there instead.


@Evk notes that EF might not support column-wise binding of tuples. If that is the case, then your best bet would be to create a POCO that matches the column definitions:

class Foo // rename me to something meaningful
{
    // note: there may be custom attributes you can use
    // to make these names less ugly, i.e.
    // [Column("TEXT")] on a property called Text
    public DateTime RMA_DATUM {get;set;}
    public string TEXT {get;set;}
    public string BEMERKUNG {get;set;}
}

and use SqlQuery<Foo>.

Sign up to request clarification or add additional context in comments.

3 Comments

I doubt this will work. Tuple doesn't have default constructor. ValueTuple does, but anyway it has properties with names like Item1 etc, while columns returned by OP select statement have different names.
@Evk I'm not 100% sure of the capabilities of EF here, but certainly some ORMs/micro-ORMs detect tuples/value-tuples, and implement column-wise member binding. If EF doesn't do this, then indeed: it would be problematic
Well, it doesn't.

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.