How to convert void to implicitly local variable?

时间:2016-10-20 18:52:18

标签: c# asp.net-mvc

Here I'm writing logic in class file and implementing in a controller. When I'm trying to implement the code in the controller it's throwing an error as "cannot assign void to an implicitly-typed local variable"

public void Getass()
        {
            var xx = from n in db.Accessors
                     join cn in db.Countrys on n.CountryID equals cn.CountryID
                     select new
                     {
                n.Name,n.Id,n.CountryID,
                                    cn.CountryName};
        }

Dummy.cs

 public JsonResult tt()
        {
             var  sss=  objrepo.Getass();

            return new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

3 个答案:

答案 0 :(得分:1)

Getass should return the collection but it is an anonymous type. Either create it in the method making the call

public JsonResult tt()
{
     var xx = from n in db.Accessors
              join cn in db.Countrys on n.CountryID equals cn.CountryID
              select new
              {
                 n.Name,
                 n.Id,
                 n.CountryID,
                 cn.CountryName
              };

    return Json(xx, JsonRequestBehavior.AllowGet);
}

or create a class to hold the result

public class MyModel {
    public string Name { get; set; }
    public string Id { get; set; }
    public string CountryID { get; set; }
    public string CountryName { get; set; }
}

public IList<MyModel> GetAccessors()
{
    var xx = from n in db.Accessors
             join cn in db.Countrys on n.CountryID equals cn.CountryID
             select new MyModel 
             {
                 Name = n.Name,
                 Id = n.Id,
                 CountryID = n.CountryID,
                 CountryName = cn.CountryName
             };
    return xx.ToList();
}

public JsonResult tt()
{
     var  sss=  objrepo.GetAccessors();

    return Json(sss, JsonRequestBehavior.AllowGet);
}

答案 1 :(得分:0)

You'll need to create a concrete type to return that from your method. void means the method doesn't return anything, and you can't use var as a return type.

So your choices are:

  • Create a concrete type and have your query create objects of that type (and change your return type to IEnumerable<{type}> or similar
  • Put the query from Getass inside the tt method so you can use an anonymous type.

答案 2 :(得分:0)

you can't return a void type and expecting a result that you can assign to a variable

more you are creating an anonymous type by doing this

  select new  {
                n.Name,n.Id,n.CountryID,
                                    cn.CountryName};

if you want to return it oops, you can't return an anonymous type because you haven't the name at compilation time. c# will assign an arbitrary name once compiled so you have to use object type

You can only return object, or container of objects, e.g. IEnumerable<object>, IList<object>, etc.