我想创建一个存储过程,在proc的WHERE部分有一个可选参数.我的C#代码可以传入null或此proc的有效产品ID.这里是:
declare @ProductID int
set @ProductID = null
select
*
from
Products
where
Name like '%Hasbro%'
if @ProductID is not null
begin
and ProductID = @ProductID
end
Run Code Online (Sandbox Code Playgroud)
此代码不起作用.如果产品ID为null,我希望它只查找名为'Hasbro'的产品.如果产品ID不为null,我希望它在其中查找名称为'Hasbro'的产品以及匹配产品ID.
谢谢!
更新:
这是工作代码.谢谢大家帮我搞定!
declare @ProductID int
set @ProductID = null
select
*
from
Products
where
Name like '%Hasbro%'
and (( @ProductID is null ) or ( @ProductID is not null and ProductID = @ProductID ))
Run Code Online (Sandbox Code Playgroud) 我需要帮助过滤实体框架(EF)导航属性上的LINQ查询.首先创建表(不是代码优先).
对于这个问题,让我们使用Office中的字符.
假设我有以下表格和列:
Offices
Id
Name
Location
Employees
Id
Name
EmployeeTypes
Id
Description
Employees_EmployeeTypes
Id
EmployeeId
EmployeeTypeId
Run Code Online (Sandbox Code Playgroud)
表格包含以下数据:
Offices
1 North Branch PA
2 South Branch FL
3 East Branch NY
Employees
1 Jim
2 Pam
3 Oscar
4 Dwight
5 Michael
6 Angela
7 Kevin
8 Stanley
EmployeeTypes
1 Manager
2 Salesman
3 Assistant
4 Receptionist
5 Accountant
Employees_EmployeeTypes
1 1 2
2 2 4
3 3 5
4 4 2
5 5 1
6 6 5
7 7 …Run Code Online (Sandbox Code Playgroud) 我有一个js小提琴(位于这里),我想模仿使用knockout.js.这个想法是每个按钮都有一个相应的div标签.如果相应的div标签可见,则在单击按钮时应隐藏.否则,它应该显示.如果任何其他非对应的div可见,则应隐藏然后显示相应的div.如何使用knockout模仿这个jQuery版本?淘汰版的js小提琴就在这里.它有效,但看起来仍然很冗长.似乎应该有一种方法可以使它更具活力并减少工作量.任何帮助是极大的赞赏.
<style type="text/css">
.text { background-color: lightgray; }
</style>
<script type="text/javascript">
$(document).ready(function () {
var viewModel = {
showHide1: ko.observable(false),
showHide2: ko.observable(false),
showHide3: ko.observable(false),
toggle1: function () {
this.showHide1(true);
this.showHide2(false);
this.showHide3(false);
},
toggle2: function () {
this.showHide1(false);
this.showHide2(true);
this.showHide3(false);
},
toggle3: function () {
this.showHide1(false);
this.showHide2(false);
this.showHide3(true);
}
};
ko.applyBindings(viewModel);
});
</script>
<div id="text1" class="text" data-bind="if: showHide1">Text 1</div>
<div id="text2" class="text" data-bind="if: showHide2">Text 2</div>
<div id="text3" class="text" data-bind="if: showHide3">Text 3</div>
<br />
<br />
<button id="button1" …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的WPF应用程序,用于一次在一个图像中预览任何给定文件夹中的图像.您可以将其视为Windows Image Viewer克隆.该应用程序有一个PreviewKeyUp事件,用于加载文件夹中的上一个或下一个图像,如果按下左箭头或右箭头键.
<Grid>
<Image x:Name="CurrentImage" />
</Grid>
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Left:
DecreaseIndex();
break;
case Key.Right:
IncreaseIndex();
break;
}
var currentFile = GetCurrentFile();
CurrentImage.Source = new BitmapImage(new Uri(currentFile));
}
Run Code Online (Sandbox Code Playgroud)
我试图解决的问题是,在加载多个图像直到垃圾收集发生时,存在大量内存膨胀.您可以在我拍摄的应用程序内存使用情况的屏幕截图中看到这一点.在垃圾收集发生之前,它超过300 MB并不罕见.
我尝试在using语句中包装图像,但这不起作用,因为BitmapImage不实现IDisposable.
using (var image = new BitmapImage(new Uri(currentFile)))
{
CurrentImage.Source = image;
}
Run Code Online (Sandbox Code Playgroud)
将多个图像加载到我的应用程序时,我该怎么做才能防止内存膨胀?
我第一次使用ASP.NET图表并取得了巨大的成功.我想要做的一件事就是放大我的图表,使y值不会从0到100.例如,假设我有一些从72到89的点值.我想做的是y轴的y值最低为72,y轴的y值最高为89(它当前显示0为最低值, 100为最高).这是我正在使用的代码:
<asp:Chart ID="Chart1" ImageLocation="~/content/images/temp/charts/ChartPic_#SEQ(300,3)" Height="325px" Width="900px" runat="server">
<Titles>
<asp:Title Text="Overview" Font="Arial, 12pt, style=Bold" />
</Titles>
<Legends>
<asp:Legend Font="Segoe UI, 8pt" Alignment="Center" BorderWidth="1" BorderDashStyle="Solid" BorderColor="#C6C6C6" Docking="Bottom" />
</Legends>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<AxisY LineColor="#C6C6C6" IsInterlaced="true" InterlacedColor="#F0F0F0">
<LabelStyle Font="Segoe UI, 8pt" ForeColor="#787878" />
<MajorGrid LineColor="#C6C6C6" />
</AxisY>
<AxisX LineColor="#C6C6C6">
<LabelStyle Font="Segoe UI, 8pt" ForeColor="#787878" />
<MajorGrid LineColor="#C6C6C6" />
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
protected void Page_Load(object sender, EventArgs e)
{
var series = new Series("Overview")
{
Name = "Series1",
ChartType = SeriesChartType.Line,
MarkerStyle …Run Code Online (Sandbox Code Playgroud) 我正在使用带有自定义分页的Telerik RadGrid.我的问题是,当我手动设置页面大小时,会触发PageSizeChanged事件并将我的代码抛入无限循环.作为一个黑客,我创建了一个在触发PageSizeChanged事件时设置的布尔值.如何在不触发PageSizeChanged事件的情况下在RadGrid控件上设置页面大小?
private bool PageSizeChanged
{
get { return Convert.ToBoolean(ViewState["PageSizeChanged"]); }
set { ViewState["PageSizeChanged"] = value; }
}
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
if (!PageSizeChanged)
{
PageSizeChanged = true;
RadGrid1.PageSize = e.NewPageSize;
}
PageSizeChanged = false;
RadGrid1.Rebind();
}
Run Code Online (Sandbox Code Playgroud)
更新:我现在有一个解决方案.我最终从调用列表中删除了事件处理程序,设置了页面大小,然后将事件添加回调用列表.
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
RadGrid1.PageSizeChanged -= RadGrid1_PageSizeChanged;
RadGrid1.PageIndex = 0;
RadGrid1.PageSize = e.NewPageSize;
RadGrid1.Rebind();
RadGrid1.PageSizeChanged += RadGrid1_PageSizeChanged;
}
Run Code Online (Sandbox Code Playgroud) 我目前正在我的jQuery代码中编写非常明确的选择器.例如给出这个标记
<div class="codeblue">
<div class="codeyellow">
<div class="codeorange">
<div class="codewhite">
<select id="codeChoice">
<option>red</option>
<option>green</option>
<option>black</option>
</select>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我使用这个显式选择器
var $select = $('.codeblue .codeyellow .codeorange .codewhite #codeChoice');
Run Code Online (Sandbox Code Playgroud)
这样做会更好吗?
var $codeBlue = $('.codeblue');
var $select = $codeBlue.find('#codeChoice');
Run Code Online (Sandbox Code Playgroud)
没有使用显式选择器是否有任何性能命中?
我想使用动态linq匹配对象的属性.在我的例子中,我有一个有两个属性的Turtle类.在未来,我可能会给它更多的属性.我有一个FilterTurtles()方法,它绑定到Turtle类的属性,不可扩展.我想使用动态linq使其可扩展.例如,假设我想通过Name"Gilly"和Color"Brown"进行过滤,也许还有一个名为"Breed"的未来属性.如何在FilterTurtlesWithLinq()方法中使用动态linq过滤海龟集合?
public class Turtle
{
public string Name { get; set; }
public string Color { get; set; }
}
public class Filter
{
public string Name { get; set ;}
public string Value { get; set ;}
}
public class Test
{
private List<Turtle> Turtles { get; set;}
public Test()
{
Turtles = new List<Turtle>();
Turtles.Add(
{
new Turtle { Name = "Gilly", Color = "Brown" },
new Turtle { Name = "Flow", Color = "Green" },
new Turtle …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个名为RemoveWhere的扩展方法,该方法基于谓词从List集合中删除项目.例如
var result = products.RemoveWhere(p => p.ID == 5);
我正在使用Microsoft的Where扩展方法签名作为起点.这是我到目前为止所拥有的:
public static List<T> RemoveWhere<T>(this List<T> source, Func<T, List<T>> predicate)
{
if (source == null)
{
throw new ArgumentNullException("source", "The sequence is null and contains no elements.");
}
if (predicate == null)
{
throw new ArgumentNullException("predicate", "The predicate function is null and cannot be executed.");
}
// how to use predicate here???
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用谓词.有人可以帮我完成这个吗?谢谢!
这个问题涉及C#,LINQ分组和集合.
我目前正在处理分组问题,我想从社区获得一些反馈.我过去曾经遇到过这个特殊问题,我正在考虑为它编写自己的数据结构.以下是详细信息:
假设您有一个由制造商和产品组成的分组,并且数据结构按制造商分组.有许多制造商和许多产品.每个制造商都有一个独特的名称和ID.产品可能具有相似的名称,但它们具有唯一的ID.进程列表代表一个例子.
福特1,嘉年华1945,金牛座6413,融合4716,F1 8749,
丰田2,凯美瑞1311,普锐斯6415,卡罗拉1117,塔科马9471
雪佛兰3,Silverado 4746,Camero 6473,Volt 3334,Tahoe 9974
等等...
我将用来表示这个数据结构
IEnumerable<Manufacturer, ManufacturerID, IEnumerable<Product, ProductID>>
Run Code Online (Sandbox Code Playgroud)
但这不存在.所以我的问题我想问社区,你会推荐什么样的数据结构?为什么?
更新:
我想保持匿名类型并避免使用dynamic关键字.所以数据结构就像这样
IEnumerable SomeDataStructure<T, U, V>
另一个要求是可以有重复的项目.这就是我在想的:
public class MyDataStructure<T, U, V>
{
// make this like a list, not a dictionary
}
Run Code Online (Sandbox Code Playgroud)
更新:
我决定使用Tuple数据结构.它非常强大且易于查询.程序代码是我最终用它来创建我的制造商 - 车辆关系的方式.结果是一个精心排序的数据结构,其中有独特的制造商按名称排序,并按名称排序相关的独特车辆.
public class ManufacturersVehicles
{
public int ManufacturerID { get; set; }
public string ManufacturerName { get; set; }
public int VehicleID { get; set; }
public string VehicleName { get; set; }
}
// "data" …Run Code Online (Sandbox Code Playgroud) 很抱歉打败了一匹死马,但我需要一些关于将内容集中在div标签内的建议.我有一个div标签,里面有一个表(带有上下文类的div).我有一个对齐属性,它产生我想要的结果,但我想用css正确地做到这一点.如果我将div的'text-align'设置为'center',它会向下传播到表格单元格中,这不是我想要的.那么......如何在不影响表格单元格的情况下居中div的内容?这是这段代码的小提琴.
<style type="text/css">
* { font-family: Arial; font-size: 12px; }
.container { padding-bottom: 10px; }
.content { background-color: #EEEEEE; }
</style>
<div class="container">
<div>Demo</div>
<div class="content" align="center">
<table>
<tbody>
<tr>
<td>Item 1</td>
<td style="padding-left: 10px;"><a href="javascript:void(0);">remove</a></td>
</tr>
<tr>
<td>Item 2</td>
<td style="padding-left: 10px;"><a href="javascript:void(0);">remove</a></td>
</tr>
<tr>
<td>Item 3</td>
<td style="padding-left: 10px;"><a href="javascript:void(0);">remove</a></td>
</tr>
<tr>
<td>Item 4</td>
<td style="padding-left: 10px;"><a href="javascript:void(0);">remove</a></td>
</tr>
<tr>
<td>Item 5</td>
<td style="padding-left: 10px;"><a href="javascript:void(0);">remove</a></td>
</tr>
</tbody>
</table>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在MVC 5中使用路由属性.我在Visual Studio中创建了一个空的MVC项目进行实验,到目前为止我无法使路由工作.我正在使用此页面作为参考.我有最新的程序集,并将所有NuGet软件包更新到最新版本.
这是我的代码:
// RouteConfig.cs
namespace MvcApplication1
{
using System.Web.Mvc;
using System.Web.Routing;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Enables MVC attribute routing.
routes.MapMvcAttributeRoutes();
// The default route mapping. These are "out of the bag" defaults.
routes.MapRoute(null, "{controller}/{action}/{id}", new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
}
}
// TestControler.cs
namespace MvcApplication1.Controllers
{
using System.Web.Mvc;
public class TestController : Controller
{
public ContentResult Output1()
{
return Content("Output …Run Code Online (Sandbox Code Playgroud) 我有一个LINQ分组,我想用它来填充父和子转发器控件.
<asp:Repeater ID="Parent" runat="server" OnItemDataBound="Parent_ItemDataBound">
<ItemTemplate>
<%# Eval("Key") %>
<asp:Repeater ID="Child" runat="server">
<ItemTemplate>
<%# Eval("Id") %>
<%# Eval("Name") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
public class Dog
{
public int Id { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
}
private IEnumerable<IGrouping<string, Dog>> GetDogs()
{
var dogs = new List<Dog>
{
new Dog
{
Id = 1,
Name = "Rex",
Breed = "Poodle",
},
new Dog
{
Id = 2,
Name …Run Code Online (Sandbox Code Playgroud) c# ×10
asp.net ×4
html ×4
linq ×4
.net ×2
javascript ×2
jquery ×2
sql ×2
webforms ×2
asp.net-mvc ×1
binding ×1
charts ×1
collections ×1
css ×1
database ×1
image ×1
knockout.js ×1
sql-server ×1
telerik ×1
wpf ×1