C# :

C# :,c#,sql,linq,asp.net-mvc-3,C#,Sql,Linq,Asp.net Mvc 3,以下是更改为公共的方法: public int getComodityCount(string param_Name, string param_ComodityModel, string param_SellerName) { return db.Comodity.Where(x => x.Name == param_Name && x.ComodityModel == param_ComodityModel && x.Se

以下是更改为公共的方法:

    public int getComodityCount(string param_Name, string param_ComodityModel, string param_SellerName)
    {
        return db.Comodity.Where(x => x.Name == param_Name && x.ComodityModel == param_ComodityModel && x.SellerName == param_SellerName).Count();
    }
以下是您查看的代码:

    @{
        int count = new ComodityController().getComodityCount("iPhone","3gs","Neal");
    }
我希望这有帮助。Where(linq)方法对于数据库查询非常方便

public class Comodity
{
    public int ID { get; set; }

    [Required(ErrorMessage = "Name is required")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Date is required")]
    //[DisplayFormat(DataFormatString = "{0:d}")]
    [DataType(DataType.Date)]
    public DateTime ? RegisterDate { get; set; }

    [Required(ErrorMessage = "Comodity Model must be specified")]    
    public string ComodityModel { get; set; }

    [Required(ErrorMessage = "Color must be specified")]
    [StringLength(15)]
    public string Color { get; set; }

    [Required(ErrorMessage = "Price Required")]
    [Range(1, 1000, ErrorMessage = "Price must be between $1 and $100")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Price { get; set; }

    [Required(ErrorMessage = "Seller name must be specified")]
    [StringLength(15)]
    public string SellerName { get; set; }

    public int CountCom { get; set; }
}

public class ComodityDBContext : DbContext 
{ 
    public DbSet<Comodity> Comodities { get; set; } 
}
    public ActionResult MyView(DateTime? startDate, DateTime? endDate, string comModel, string searchString)
    {
        //   if (string.IsNullOrEmpty(comModel))
        //   {
                //     return View(comodities);
        //   }

        //   else
        //   {
           //    return View(comodities.Where(x => x.ComodityModel == comModel));
         //  }
        DateTime dtNow;
        dtNow = DateTime.Today;

        if (!startDate.HasValue)
        {
            startDate = new DateTime(dtNow.Year, dtNow.Month, 1);
            endDate = startDate.Value.AddMonths(1).AddDays(-1);
        }
        if (startDate.HasValue && !endDate.HasValue)
        {
            endDate = (new DateTime(startDate.Value.Year, startDate.Value.Month, 1)).AddMonths(1).AddDays(-1);
        }


        ViewBag.startDate = startDate.Value.ToShortDateString();
        ViewBag.endDate = endDate.Value.ToShortDateString();

        var viewDate = from r in db.Comodities
                       where r.RegisterDate >= startDate.Value && r.RegisterDate <= endDate.Value == true
                       //  orderby r.RegisterDate
                       select r.RegisterDate;

        var SelectListName = new List<string>();
        var SelectNameQry = from m in db.Comodities
                            select m.SellerName;
        SelectListName.AddRange(SelectNameQry.Distinct());
        ViewBag.searchString = new SelectList(SelectListName);

        var comModelLst = new List<string>();
        var comModelQry = from d in db.Comodities
                          orderby d.ComodityModel
                          select d.ComodityModel;
        comModelLst.AddRange(comModelQry.Distinct());
        ViewBag.comModel = new SelectList(comModelLst);

        var comodities = from m in db.Comodities
                         select m;

        IDictionary<string, IList<string>> dict = new Dictionary<string, IList<string>>();
        var queryC = from c in db.Comodities
                     group c by c.ComodityModel into g
                     where g.Count() > 1
                     select new { ComodityModel = g.Key, CCount = g.Count() };
        foreach (var item in queryC)
        { // comodities = comodities.Where(item => item.Name && item => item.CCount);
            //View("", item.ComodityModel, item.CCount);
            //  ViewBag(item.ComodityModel, item.CCount);
            String key = item.ComodityModel;
            if (dict.ContainsKey(key))
            {
        // add the class name into an existing "string" collection
                 dict[key].Add(item.ComodityModel);
            }
            else
            {
        // instantiate a new "string" collection and add the class name.
                dict[key] = new List<string> { item.ComodityModel };
            }

            int maxCourseCount = 0;
             foreach (var k in dict.Keys)
            {
                int valueCount = dict[k].Count;
                if (valueCount > maxCourseCount)
                    maxCourseCount = valueCount;
            }
        }



        if (!String.IsNullOrEmpty(searchString))
        {
            comodities = comodities.Where(s => s.SellerName.Contains(searchString));
        }
        if (startDate.HasValue && endDate.HasValue)
        {
            comodities = comodities.Where(r => r.RegisterDate >= startDate.Value && r.RegisterDate <= endDate.Value);

        }

        if (string.IsNullOrEmpty(comModel))
        {
            return View(comodities);
        }

        else
        {
            return View(comodities.Where(x => x.ComodityModel == comModel));
        }
    }
@model IEnumerable<SaleCenter.Models.Comodity>

@{
    ViewBag.Title = "total";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<h2>total</h2>

<p>

    @using (Html.BeginForm()){          
        <p>  startdate:@Html.TextBox("startDate", null, new { @class = "datePicker" }) &nbsp;
             enddate : @Html.TextBox("endDate", null, new { @class = "datePicker" }) </p>
        <p>  model: @Html.DropDownList("comModel", "All") &nbsp; 
             seller: @Html.DropDownList("SearchString", "All")  
             <input type="submit" value="Total" /></p>    
    }
</p>
<table>
    <tr>
        <th>
            product name
        </th>
        <th>
            Model
        </th>
         <th>
            seller
        </th>
        <th>
            quantity
        </th>
        <th>
            graphic
        </th>
    </tr>

@foreach (var item in Model)
{
    int w = (int)(2000 * item.CountCom / 100); 
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ComodityModel)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SellerName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CountCom)
        </td>

      <td>
      <img src="@Url.Content("~/Content/Images/graph.gif")" width="@w.ToString()px" height="6px"  alt="" />

        </td>

    </tr>
}

</table>

<script type="text/javascript">
    $(document).ready(function () {

        $('.datePicker').datepicker({ firstDay: 1, dateFormat: 'dd.mm.yy', showOn: 'button',
            buttonImage: '/Content/images/calendar.gif',
            duration: 0
        });
    });
</script>
    private int getComodityCount(string param_Name, string param_ComodityModel, string param_SellerName)
    {
        return db.Comodity.Where(x => x.Name == param_Name && x.ComodityModel == param_ComodityModel && x.SellerName == param_SellerName).Count();
    }
    int count = getComodityCount("iPhone","3gs","Neal");
    public int getComodityCount(string param_Name, string param_ComodityModel, string param_SellerName)
    {
        return db.Comodity.Where(x => x.Name == param_Name && x.ComodityModel == param_ComodityModel && x.SellerName == param_SellerName).Count();
    }
    @{
        int count = new ComodityController().getComodityCount("iPhone","3gs","Neal");
    }