小编LCJ*_*LCJ的帖子

检查两个逗号分隔的字符串是否相等(对于内容集)

我有四个字符串,如下所示.虽然它们具有不同的字符顺序和逗号后的不同间距 - 它们被认为具有same business value.

  1. 如何检查所有字符串是否相同(根据上面解释的业务场景)?我有以下代码,但在逗号后的空格情况下失败.
  2. 有什么比这更好的方法(为此目的)Enumerable.SequenceEqual

注意:"A,B"将被视为与"B,A,B,A,B"相同

注:我使用Visual Studio 2010.Net Framework 4

  string firstString = "A,B,C";
  string secondString = "C,A,B";
  string thirdString = "A,B, C";
  string fourthString = "C, A,B";


  //Set 1 Test
  List<string> firstList = new List<string>(firstString.Split(','));
  List<string> secondLsit = new List<string>(secondString.Split(','));
  bool isStringsSame = Enumerable.SequenceEqual(firstList.OrderBy(t => t), secondLsit.OrderBy(t => t));
  Console.WriteLine(isStringsSame);


  //Set 2 Test
  List<string> thirdList = new List<string>(thirdString.Split(','));
  List<string> fourthList = new List<string>(fourthString.Split(','));
  bool isOtherStringsSame = Enumerable.SequenceEqual(thirdList.OrderBy(t => …
Run Code Online (Sandbox Code Playgroud)

c# linq

1
推荐指数
1
解决办法
3096
查看次数

如何从Repeater获取更新的Textbox值?

我有一个repeater控件如下所示.它有一个textbox控件.当save button被点击时,我需要从文本框中的文字更新.我有以下代码; 但是当我拿到文本框文本时,它给了我旧的价值.

我们如何获得更新的文本?

代码背后

    protected void Save_Click(object sender, EventArgs e)
    {

        foreach (RepeaterItem item in repReports.Items )
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem )
            {

                string updatedEmail = ((TextBox)item.Controls[5]).Text;
                string originalEmail = ((HiddenField)item.Controls[7]).Value;


            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

控制标记

  <div class="repeaterTableBorder">
                <asp:Repeater ID="repReports" runat="server">
                    <ItemTemplate>
                        <div id="repeaterIdentifier" class="repeaterIdentifier">
                            <div class="reportTitle">
                                <%# Eval("ReportName") + ":"%>
                                <asp:HiddenField ID="hdnLastChangeTime" runat="server" Value= '<%# ((DateTime)Eval("RecordSelectionTime")).ToString("MM/dd/yyyy hh:mm:ss.fff tt")%>' />
                                <asp:HiddenField ID="hdnReportID" runat="server" Value='<%# Eval("ReportTypeCode")%>' />
                            </div>
                            <div class="reportFrequency"> …
Run Code Online (Sandbox Code Playgroud)

c# asp.net gridview repeater

1
推荐指数
1
解决办法
6820
查看次数

将事务结果用作参数时发生超时错误

我有以下使用SqlTransaction的代码

string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    SqlTransaction transaction = connection.BeginTransaction();

    int logID = HelperClass.InsertException(connection, 1, DateTime.Now, "Y", "test", "test", 1, 1, transaction);
    LogSearch logSearch = new LogSearch();
    logSearch.LogID = 258;

    Collection<Log> logs = HelperClass.GetLogs(logSearch, connectionString);
}
Run Code Online (Sandbox Code Playgroud)

此代码抛出以下异常.

超时已过期.操作完成之前经过的超时时间或服务器没有响应.

但是,如果我为LogID传递硬编码值,则没有例外.

  1. 当我传递logID(来自InsertException()的结果)时,为什么会出现异常?
  2. 请解释为什么我将hard coded值传递为LogID 时没有异常

注意:InsertException()使用连接,SqlTransaction而GetLogs()使用没有任何事务的新连接

更新的问题

业务层代码不使用Transaction.我需要在上面显示的单元测试代码中调用业务层方法(用于集成测试).即使业务层不使用事务,我们如何将事务应用于UT代码(用于集成测试)?从@jbl回答看来,似乎完全不可能在单元测试中使用事务.我们如何应用UT代码的交易.

public static class HelperClass
{
    public static Collection<Log> GetLogs(LogSearch logSearch, string connectionString)
    {
        Collection<Log> logs = new …
Run Code Online (Sandbox Code Playgroud)

c# concurrency ado.net transactions

1
推荐指数
1
解决办法
886
查看次数

重复的连接字符串错误

我在服务器中部署了一个网站.有一天它抛出一个错误,说connection string已经添加了名称.我检查了web.config文件,它只有一个名称.我从配置中删除了该条目.现在该网站运行良好,从数据库中获取数据.

注意:当我更改配置文件的名称时,它显示错误.

我认为,问题是 - connectionstring部分缓存在内存中.是这样吗?我们如何克服这种不受欢迎的行为?

在源代码中配置文件

发布配置

<system.web>
 <compilation xdt:Transform="RemoveAttributes(debug)" />

</system.web>
Run Code Online (Sandbox Code Playgroud)

调试配置

<system.web>

</system.web>
Run Code Online (Sandbox Code Playgroud)

参考文献:

  1. 在我发布的Web配置中出现不需要的连接字符串问题
  2. .NET 2.0 App.Config连接字符串包含不需要的SQLExpress默认值

c# asp.net connection-string config xdt-transform

1
推荐指数
1
解决办法
1485
查看次数

使用LINQ从xml创建实体对象的最佳方法

我有以下代码用于从源创建对象列表XML.我可以在var query变量中得到需求结果.List<Video>从这个结果创建一个最好的方法是什么?

注意:Method Chaining如果可能,请选择方法.

class Program
{
    static void Main(string[] args)
    {
        string xmlStringInput = @"<videoShop>
                                  <video title=""video1"" path=""videos\video1.wma""><Director>Speilberg</Director></video>
                                  <video title=""video2"" path=""videos\video2.wma""/>
                                </videoShop>";

        XDocument myDoc = XDocument.Parse(xmlStringInput);


        var videoElements = (from video in myDoc.Descendants("video") select video).ToList();
        foreach (var videoEle in videoElements)
        {
            //System.Xml.XPath namespace for XPathSelectElement
            var directorName = videoEle.XPathSelectElement(@"Director");
        }


        var query = from video in myDoc.Descendants("video")
                    select new
                    {
                        MyTitle = video.Attribute("title").Value,
                        MyPath = video.Attribute("path").Value
                    };

        //IEnumerable<XElement> elements = …
Run Code Online (Sandbox Code Playgroud)

.net c# xml linq

1
推荐指数
1
解决办法
1983
查看次数

代码间距和缩进提示

我知道这可能会引发一些讨论,而这不是希望这个网站想要的,但这个问题可以回答.

我总是以一种我开始皱眉的方式编码:

<html>

<head>

</head>

<body>

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

我把所有东西都加倍了,我从不缩进,真的.

任何人都可以告诉我如何有用的缩进,这是真的吗?另外,如果代码更好,我应该如何缩进或空格?

谢谢你然后...

如果这个问题没有用,请发表评论或建议编辑而不是评价......!

编辑:注意,这个编辑是在Daniel Imm的回答之后

我应该如何缩进CSS或Javascript或PHP等内容?

indentation spacing

1
推荐指数
1
解决办法
372
查看次数

Url.Action不适用于getJSON

我在MVC4 RAZOR视图项目中有以下jQuery代码.当我对url进行硬编码时,call to action方法正常工作.但是当我使用时Url.Action,它不起作用.

硬编码方法的结果: http://localhost:64994/Search/GetCostpages/?costPageNumber=111

Url.Action方法的结果: http://localhost:64994/@Url.Action(%22Search%22,%20%22GetCostpages%22)?costPageNumber=111

有什么需要纠正才能使它工作?

$(function ()
{
    $("#btnCostPageNumberMagnifingLens").click(function ()
    {

        var costPageNumber = $("#txtCostPageNumber").val();

        //$.getJSON('/Search/GetCostpages/',{costPageNumber: costPageNumber},

        $.getJSON('@Url.Action("Search", "GetCostpages")',{costPageNumber: costPageNumber},
                            function (data) 
                            {

                             alert('Inside Function');

                             });
    });
});
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc jquery asp.net-mvc-4

1
推荐指数
1
解决办法
3544
查看次数

将所有列读入字符串的最佳方法

我有以下代码来读取 SqlDataReader 的结果。我需要通过读取每一行来创建 resultVal 字符串。我需要读取每个列值并与 resultVal 连接。但是我不会事先知道查询结果中存在的列数。

目前我正在使用try..catch如下所示的方法,但效率不高。. 一种替代方法是使用获取 SqlDataReader 中的列数中FieldCount给出的 datareader 属性

有没有更好的方法将所有列和行值放入一个字符串中?

注意:我使用的是 .Net 2.0

using (SqlDataReader reader = command.ExecuteReader())
{
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            if (string.IsNullOrEmpty(resultVal))
            {
                resultVal = reader.GetString(0);
            }
            else
            {
                resultVal = resultVal + "___" + Convert.ToString(reader.GetValue(0));
            }

            try
            {
                if (reader.GetValue(1) != DBNull.Value)
                {
                    resultVal = resultVal + "-" + Convert.ToString( reader.GetValue(1));
                }
            }
            catch
            {
                //do nothing
            }


        }
    }
    else
    {
        //EmptyLogFunction(spStatement);
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c#

1
推荐指数
1
解决办法
2581
查看次数

图标未与按钮内的文本对齐

我有一个带有跨度的按钮元素,用于在按钮内显示图标.但他们并不一致.我提到了许多论坛帖子,他们建议中间对齐 - 但这并不能解决问题.还有其他方法可以解决这个问题吗?

小提琴

http://jsfiddle.net/Vw9Z6/432/

HTM代码

<button class="ui-button ui-widget ui-state-default app-custom-button-request"><span class="ui-button-icon-primary ui-icon ui-icon-star app-span"></span>Request</button>
Run Code Online (Sandbox Code Playgroud)

CSS

.app-custom-button-request 
{
   color:red;
   margin-left:10px; 
   display: inline-block;
   vertical-align: middle; 
   width:200px;
}
Run Code Online (Sandbox Code Playgroud)

截图

在此输入图像描述

html css jquery-ui

1
推荐指数
1
解决办法
653
查看次数

使用 POSTMAN 在 Web API 中进行模型绑定后对象为空

我正在尝试学习 Web API 并创建了我的第一个项目,如下所示。我正在使用邮递员测试它。post 方法工作正常,我收到响应消息 - 但控制器收到的 post 操作输入为空。需要做什么才能在控制器中获取 post 值?

using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        List<Comment> comments;

        // GET api/values
        public IEnumerable<Comment> Get()
        {
            return comments;
        }

        // GET api/values/5
        public Comment Get(int id)
        {
            Comment c = comments[id-1];

            if (string.IsNullOrEmpty(c.Description))
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
            }

            return c;
        }

        // POST api/values
        public HttpResponseMessage Post(Comment inputComment)
        {
            Comment c = new Comment();
            if (inputComment != null)
            { …
Run Code Online (Sandbox Code Playgroud)

rest asp.net-web-api postman

1
推荐指数
1
解决办法
2001
查看次数