-5

I am trying to retrieve some records from the database based on an ID parameter that comes in. How ever it gives me an error saying

Cannot implicitly convert type System.Collection.Generic.IEnumerable SourceModel to SourceModel.An explicit conversion exists(are you missing a cast)

This is the part of the query to connect to the db

public static class Source
{
    public static IEnumerable<SourceModel> GetRecords(string ID, Func<SourceModel,bool> criteria = null)
    {
        var command = new StringBuilder();
        command.AppendFormat("Select * from [RecordsTable] where ID={0}", ID);

        return ExecuteQuery(command.ToString());
    }
}

This is the code I tried to pass as follows

public static SourceModel GetRecord(string ID)
{
    var recs = Source.GetRecords(ID);
    return  recs; // on this line when i am trying to return the records i get the above mentioned error
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
marry
  • 153
  • 2
  • 3
  • 15
  • 2
    Hello. Can you provide a [MCVE] and indent your code? – aloisdg Jun 07 '19 at 07:25
  • Alternativly return a specific element without that collection, e.g. `recs.First()`. – MakePeaceGreatAgain Jun 07 '19 at 07:27
  • 1
    `recs` in your second method will be `IEnumerable`. You can not return that, since you are specifying that the method returns a single `SourceModel`. So either change the return object to `IEnumerable` or return a specific `SourceModel` of `recs` – Nico Schreiner Jun 07 '19 at 07:29
  • Is it correct that there are multiple elements with the same ID? – MakePeaceGreatAgain Jun 07 '19 at 07:31
  • 2
    Note that in order to be protected from SQL injection, you need to pass the query with `{X}` placeholders and the parameter values directly to [`ExecuteQuery`](https://learn.microsoft.com/en-us/dotnet/api/system.data.linq.datacontext.executequery?view=netframework-4.8#System_Data_Linq_DataContext_ExecuteQuery__1_System_String_System_Object___). Your approach with the `StringBuilder` is just as vulnerable as [direct string concatenation](https://stackoverflow.com/q/332365/11683). – GSerg Jun 07 '19 at 07:32
  • Not related to your error, but you should **NEVER** use string methods (like `StringBuilder.AppendFormat()`) to insert query parameters into SQL statements, this will open you up to [SQL Injection Attacks](https://www.owasp.org/index.php/SQL_Injection). Use this instead: `string command = "Select * from [RecordsTable] where ID={0}"; return ExecuteQuery(command, ID);` – Georg Patscheider Jun 07 '19 at 07:33

2 Answers2

1

Your GetRecords method returns an IEnumerable of SourceModel, containing 0 or more items. The GetRecord method returns a single SourceModel. So you need to convert the "0 or more" to a single result:

public static SourceModel GetRecord(string ID)
{
    var recs = Source.GetRecords(ID);
    return recs.FirstOrDefault(); 
}
Rik
  • 28,507
  • 14
  • 48
  • 67
0

Error is due to the return type of your method. Change it to return IEnumerable<SourceModel> as you are getting IEnumerable<SourceModel> from database

 public static IEnumerable<SourceModel> GetRecord(string ID)
 {
     var recs = Source.GetRecords(ID);
     return  recs; // no error now as you are returning list
 }
Voodoo
  • 1,550
  • 1
  • 9
  • 19