0

I have problem in selecting array from linQ list.

Array in general for some static data-

  List<string> nameList = new List<string>
            {
                "Jonathan", "Lisa", "Jordan", "Tyler", "Susan", "Brandon", "Clayton", "Elizabeth", "Jennifer"
            };

            var results = nameList.Where(n =>
                n.StartsWith(term, StringComparison.OrdinalIgnoreCase));

            return new JsonResult()
            {
                Data = results.ToArray(),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

Linq query-

var names = (from u in db.Contacts
                         select u).ToList();

Now from this above linq query there will be names from this column. I want to select names from this above query by making array of its name. How do I setup an array for names in this query, as it is doing for static based data.

Edit-

  [HttpGet]
        public JsonResult GetNames(string term)
        {
            var names = (from u in db.Contacts
                         where u.name.Contains(term)
                         select u).ToArray();
            // A list of names to mimic results from a database

            var results = names.Where(n =>
                n.name.StartsWith(term, StringComparison.OrdinalIgnoreCase));

            return new JsonResult()
            {
                Data = results.ToArray(),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

I have modified this method from above , Now I can get list on the server's side. But at json result at it leaves me with internal server error (500).

Script-

<script type="text/javascript">
    $(function () {
        $('#typeahead').typeahead({
            source: function (term, process) {
                var url = '@Url.Content("~/Invoice/GetNames")';

                return $.getJSON(url, { term: term }, function (data) {
                    alert(data);
                    return process(data);
                });
            }
        });
    })
</script>
4
  • can you provide error message? Commented Apr 8, 2014 at 7:40
  • @Grundy, A circular reference was detected while serializing an object of type Commented Apr 8, 2014 at 8:08
  • 2
    possibly if you need only contact names better use select u.name instead of select u, also in this case should not be circular references Commented Apr 8, 2014 at 8:24
  • @Grundy, That was probably I was missing. Glad it worked now. Commented Apr 8, 2014 at 8:37

2 Answers 2

2

It's simply :

var names = (from u in db.Contacts
            select new { Name = u.Name}).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

0

Try

var names = db.Contacts.Select(x => x.PROP_NAME).ToArray();

1 Comment

try it :return Json(names,JsonRequestBehavior.AllowGet);

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.