0

I have the following function that searches a database for entries where a column called "description" have the same value. Right now it just returns the first value it finds or a default value is there isn't one.

public static NewCode GetAltCode(int altCodeVer, string descrip)
    {
        var sql = @"select Code, Description, VersionID from Code.CodeLookup where versionid=@vers and description=@description";

        return ObjectFactory.GetInstance<IDatabaseFactory>().Query<NewCode>(sql, new { vers = altCodeVer, description = descrip, }).FirstOrDefault();
    }

I have this if statement to check and make sure the result isn't null, and if it is, to say that the "code isn't found"

[Authorize(parentAction: "Edit")]
        public ActionResult Lookup(string Code, int? VersionId = null)
        {
            var Info = VisitViews.GetDescriptionByVersionId(Code, VersionId.HasValue ? VersionId.Value : 9);
            var description = string.Empty;

            // CHECK FOR NULL
            if (Info != null)
            {
                description = Info.Description;
                if (VersionId == 9)
                {
                    var altInfo = VisitViews.GetAltCode(10, description);
                }
                if (VersionId == 10)
                {
                    var altInfo = VisitViews.GetAltCode(9, description);
                }
            }
            else
                description = "CODE NOT FOUND";

            return Json(new { Description = description });
        }

My question is, instead of doing FirstOrDefault, is there a way to store the results in an array (or even to store them in a list and call ToArray on the list)? I'm trying to get all of the codes received during the sql search instead of just one so that another function I am working on can traverse the array and place the items where they need to be in a UI.

6
  • You should be able to call ToList or ToArray instead of FirstOrDefault. Is that giving you an error? Commented Aug 4, 2014 at 15:42
  • .ToArray() and .ToList() are both giving me an error saying I can't convert type "....Code[]" to "....Code" Commented Aug 4, 2014 at 15:49
  • Change your return type. Commented Aug 4, 2014 at 15:50
  • Without changing the way what is written? Do you mean without adding ToArray? If so, then no. You could change the return type to IEnumerable<NewCode> and then eliminate the FirstOrDefault(). Commented Aug 4, 2014 at 17:10
  • I see what you are saying. I changed the return type to NewCode[] and then was able to use .ToArray(). Thanks! Commented Aug 4, 2014 at 17:16

1 Answer 1

1

For future reference of this post, here is the answer:

Change the return type to NewCode[] and replace .FirstOrDefault() with .ToArray()

public static NewCode[] GetAltCode(int altCodeVer, string descrip)
{
    var sql = @"select Code, Description, VersionID from Code.CodeLookup where versionid=@vers and description=@description";

    return ObjectFactory.GetInstance<IDatabaseFactory>().Query<NewCode>(sql, new { vers = altCodeVer, description = descrip, }).ToArray();
}
Sign up to request clarification or add additional context in comments.

4 Comments

IEnumerable<NewCode> would be better.
Why? The type in his example was NewCode.
-that should have been IEnumerable<NewCode> (edited).
It really depends on how it is being used... which we have no way of knowing. Typically, I would agree, though.

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.