0

I have a scalar function in a database that returns string value and I want to fetch this value at the server-side through LINQ. PS. The application I am developing is in .Net Core.

I tried different ways that are available over the internet to achieve this but found no luck.

Below are the links which I tried implementing

  1. http://anthonygiretti.com/2018/01/11/entity-framework-core-2-scalar-function-mapping/
  2. https://forums.asp.net/t/2055038.aspx?scalar+function+entity+framework
  3. Entity Framework Code-First Execute Scalar-Valued Functions

First try:

var query = _context.StringValue.Select(d => Context.GetDetail(form.ValueString, "State"));
string x = query.FirstOrDefault();
                        

Second try:

IQueryable<UDFResult> state = _context.Query<UDFResult>().FromSql($"select dbo.GetDetail('{form.ValueString}','State') AS value");
List<UDFResult> l = state.ToList(); 

Any help will be appreciated and also I would be interested in seeing an example of how to achieve this.

blank Result in second try

Community
  • 1
  • 1
  • What issue you are facing here? – Chetan Nov 01 '19 at 04:42
  • @ChetanRanpariya, I am not able to retrieve value from the query and getting exceptions. When I fire below query in SQL "select dbo.GetDetail('{form.ValueString}','State') AS value" I get the required response. Now I am trying to get the same response via LINQ. – Shivam Poojara Nov 01 '19 at 05:29
  • Can you share what exceptions you are getting and what are the messages of the exceptions? – Chetan Nov 01 '19 at 05:57
  • You tried some things (good!), but what were the results? No answer, wrong answer, exception, compile error? Please provide details (as an edit) – Hans Kesting Nov 01 '19 at 06:09
  • @ChetanRanpariya, First try: Exception: Invalid object name 'StringValue'. But I have already declared "StringValue" in DbContext file like below public virtual DbSet StringValue { get; set; } Second try: IQueryable state = _context.Query().FromSql($"select dbo.GetDetail('{form.ValueString}','State') AS value"); List l = state.ToList(); Not getting exception but the value in result is blank. Any help will be appriciated and also I would be interested in seeing an example of how to achieve this. – Shivam Poojara Nov 01 '19 at 06:29
  • @ChetanRanpariya, I have also added image regarding blank result in the question. – Shivam Poojara Nov 01 '19 at 06:37

0 Answers0