Problem with sql query (sqlite-net-pcl)

Václav Ornst 21 Reputation points
2023-04-25T18:06:57.65+00:00

Problem with Sql query here i have a code snippet i wonder where is the error: here is accountId = a048d740-99c0-4df4-8949-d12feee9d552 this code works: // THIS CODE WORKS

Accounts? account = serverDbConnection.Query<Accounts>($"SELECT * FROM {nameof(Accounts)} WHERE LOWER({nameof(Accounts.Email)})=LOWER(?) AND {nameof(Accounts.AccountId)}=?", email.ToLower(), accountId).FirstOrDefault<Accounts>();

// THIS CODE DOES NOT WORK

Accounts? account = serverDbConnection.Query<Accounts>($"SELECT * FROM {nameof(Accounts)} WHERE LOWER({nameof(Accounts.Email)}) = LOWER(@Email) AND {nameof(Accounts.AccountId)} = @AccountId", new
{
     Email = email.ToLower(),
     AccountId = accountId.ToString()
}).FirstOrDefault<Accounts>();even this does not work still error: System.NotSupportedException: Cannot store type: <>f__AnonymousType0`2[System.String,System.String]
Developer technologies .NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2023-04-26T08:12:45.1966667+00:00

    @Václav Ornst, Welcome to Microsoft Q&A, according to the doc, we need to use an object array as the second parameter of Query method.

    You could try the following code to solve your problem:

       Accounts? account = db.Query<Accounts>($"SELECT * FROM {nameof(Accounts)} WHERE LOWER({nameof(Accounts.Email)}) = LOWER(@Email) AND {nameof(Accounts.AccountId)} = @AccountId", new object[] { email,accountId}  
                ).FirstOrDefault<Accounts>();
    
    

    Based on my test, it could get the correct result.

    Best Regards,

    Jack

    If the answer is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.