小编Ler*_*ica的帖子

使用CodeIgniter活动记录的多个联接的SQL查询

我有这个工作查询:

$q = $this->db->query('SELECT u.name  FROM users u 
JOIN user_group ug ON u.id = ug.user_id 
JOIN groups g ON g.id = ug.group_id WHERE ug.group_id = "4" ');
Run Code Online (Sandbox Code Playgroud)

我想用活动记录替换它.我想出了类似的东西,但显然它不起作用:

$this->db->select('name');
   $this->db->from('users');
   $this->db->join('user_group', 'user_group.user_id = users.id);
   $this->db->join('groups', 'groups.id = user_group.group_id');
   $q = $this->db->get();
Run Code Online (Sandbox Code Playgroud)

谢谢Leron

sql activerecord codeigniter

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

C# - 如何使用索引将部分字符串输出到控制台

我正在编写一个简单的算法,只要满足某个条件,就可以在控制台中写入.这是我的代码:

public static void StringTest()
{
    string stringToUse = "Ala BalaB JiBBerish Ala Jibberish Ala BalaB";

    int strLength = stringToUse.Length;
    int i = 0;
    while(i < strLength-4)
    {
        if (stringToUse[i] == stringToUse[(i + 4)] && stringToUse[(i + 1)] == stringToUse[(i + 3)])
            System.Console.WriteLine(stringToUse[i] + stringToUse[(i + 1)] + stringToUse[(i + 2)] + stringToUse[(i + 3)] + stringToUse[(i + 4)]);

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

但相反,文字字符串的输出是数字434.我应该如何格式化输出以获得实际的字母,如果有人可以告诉我为什么我没有得到错误和那些数字呢?

c# string

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

C# - 使用泛型类型的方法的实现

我正在创建一个MDI form,我有一个加载不同形式的方法.现在我需要做一些修改 - 我需要添加从另一个子窗体中调用一个子窗体的功能.

因为我需要在几个不同的地方使用它,所以我创建了一个新类,其中所有需要此功能的类都继承.我希望把它与一般类型的工作,所以我可以通过我可能需要像每一个窗体类LoadAForm(MyForm1)LoadAForm(MyForm2)等..我希望我很清楚我想要的最终结果.

我试过这个:

protected void LoadAForm<T>(ref T sender)
{
    MainForm frm = this.MdiParent as MainForm;
    T temp;
    if (frm != null)
    {
        sender  = SingletonFormProvider.GetInstance<temp>(frm, true);
        sender.MdiParent = frm;
        sender.Dock = DockStyle.Fill;
        sender.Show();
    }
}
Run Code Online (Sandbox Code Playgroud)

这不起作用.但是当我在方法中使用时,我几乎没有使用泛型的经验,所以我不知道如何继续.

我使用这种语法得到的错误是The type or namespace "temp" could not be found...". I'm not even sure that this is the way to do it.GetInstance <>`必须采用与我正在调用的表单类型相同类型的参数.

c# generics winforms

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

C#将long if语句转换为更合适的语句

我有一个MDI form地方,一些儿童形式需要一个消息框显示在关闭之前,其他人可以关闭而不问.因为application.Exit()close event子表单调用时遇到问题,我处理close event父表单并检查它被触发的位置.如果它是以需要消息框的形式触发的,我会调用它,否则只需关闭应用程序.所有这些都在此代码中实现:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SEdit se       = this.ActiveMdiChild as SEdit;
            SoEdit soleEdit = this.ActiveControl as SoEdit;
            UppEdit ue      = this.ActiveControl as UpEdit;
            MEdit mat  = this.ActiveControl as MEdit;
            LEdit lse      = this.ActiveControl as LEdit;
            CEdit cle    = this.ActiveControl as CEdit;

            if (se != null || soleEdit != null || ue != null || mat != null || lse != null || cle != null)
            { …
Run Code Online (Sandbox Code Playgroud)

c# refactoring

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

C# - finally子句中的异常处理

标题有点误导,但这个问题对我来说似乎非常简单.我有try-catch-finally阻止.我只想在finally块中抛出异常时才在块中执行代码try.现在代码的结构是:

try
{
  //Do some stuff
}
catch (Exception ex)
{
  //Handle the exception
}
finally
{
  //execute the code only if exception was thrown.
}
Run Code Online (Sandbox Code Playgroud)

现在我能想到的唯一解决方案就是设置一个标志:

try
{
  bool IsExceptionThrown = false;
  //Do some stuff
}
catch (Exception ex)
{
  IsExceptionThrown = true;
  //Handle the exception   
}
finally
{
if (IsExceptionThrown == true)
  {
  //execute the code only if exception was thrown.
  }
}
Run Code Online (Sandbox Code Playgroud)

并不是说我看到了一些不好的东西,但想知道是否有另一种(更好的)方法来检查是否存在抛出的异常?

c# exception-handling

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

C# - 在catch子句中使用return

我在这里看到了类似的问题,但仍有一些我不明白的事情.据我所知,当你使用try-catchblock时,如果抛出异常,catch块将立即执行,并且不会执行catch相同代码块中的子句之后的代码.所以,如果我做到了,如果我们有:

try
{
    // do something
    // throw an exception for some reason
}
catch (Exceptiox ex)
{
    // do something when there is and exception thrown
}
// some code that will never be runned if and exception was thrown above
Run Code Online (Sandbox Code Playgroud)

我不是100%确定catch停止在其范围之外进一步执行,但这是我的一个问题,所以如果我错了,请纠正我.

那么,如果你根本不需要返回任何值,那么returncatch块中使用的重点是什么?我在一些方法中看到这个来自我正在研究的继承代码.例如:

public void DeleteImage(AppConfig imageInfo, string imageName)
{
    string imgPath = imageInfo.ConfigValue.ToString();
    try
    {
        File.Delete(imgPath + "\\" + imageName);
    }
    catch (Exception ex)
    {
        logger.Error(ex.ToString());
        return;
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# try-catch

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

使用Razor视图在asp.net mvc 3中使用预定义值创建DropDown

我正在形成一个局部视图.在其中我想使用3个值显示下拉列表,例如:

<td>
    @Html.DropDownList("Yes", "No", "Not Applicable")
</td>
Run Code Online (Sandbox Code Playgroud)

显然我不能硬编码这样的值,但这是个主意.我将仅在此视图中使用此下拉列表,因此我想在可能的情况下保留逻辑,唯一的是我想跟踪所选值,因此我想添加一些隐藏值Id,例如.在我看来有没有办法做到这一点?我想过传递一个ViewBag参数或类似的东西,但我真的认为必须有一个更优雅的解决方案.

c# razor asp.net-mvc-3

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

客户端验证使用部分视图

我刚刚为我的表单完成了可视化逻辑,现在我想使用asp.net mvc 3提供的客户端验证.然而,即使我遵循一些例子,我也无法使它工作,我不知道可能是什么原因.

这是我的主要观点:

@model List<DataAccess.MCS_DocumentFields>

@{
    ViewBag.Title = "Documents";
}

<div id="drawForm">
@using (Html.BeginForm("RecieveDataFromDocument", "Forms", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <table border="1">
        <colgroup>
            <col span="1" style="width: 10%;" />
            <col span="1" style="width: 40%;" />
            <col span="1" style="width: 25%;" />
            <col span="1" style="width: 25%;" />
        </colgroup>
        @Html.Partial("_PartialHeader", Model)
        @Html.Partial("_PartialDrawing", Model)
        @Html.Partial("_PartialBody", Model)
        @Html.Partial("_PartialFooter", Model)
    </table>
    if (ViewBag.Status == 1)
    {
        <button type="submit">Save</button>   
    }
    else
    { 
        @Html.ActionLink("Back", "Index")
    }
}
</div>
Run Code Online (Sandbox Code Playgroud)

实际上并不太多.大多数逻辑都在我的偏见中.我使用数据注释,所以我认为默认情况下我会进行一些客户端验证,但似乎并非如此.我所做的就是确保拥有

<appSettings>

  <add key="UnobtrusiveJavaScriptEnabled" value="true" />

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

添加到我的web.config.在我看来,你可以看到我已经添加了

@Html.ValidationSummary(true)
Run Code Online (Sandbox Code Playgroud)

不确定这是否适合它,但它就在那里.同样在我从那里看的例子是:

<div class="editor-label">

    @Html.LabelFor(model …
Run Code Online (Sandbox Code Playgroud)

validation jquery razor asp.net-mvc-3

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

单击span标记内的图像时,使用jQuery toggleClass()

我正在使用Razor开发ASP.NET MVC3项目.我正在尝试实现一个简单的图库.我的图像在span标记内,当单击图像时,我想仅为单击的图像切换span标记的类.

这是我的代码:

<span class="document-image-frame">
    @if (image != null) {
        <img src="file:\\105.3.2.2\upload\@image.Name" alt="docImg" />
    }
</span>
Run Code Online (Sandbox Code Playgroud)

我试过这个脚本切换:

$('.document-image-frame img').click(function () {
    $(this).toggleClass();
})
Run Code Online (Sandbox Code Playgroud)

它不起作用.我想在这种情况下$(this)是图像本身而不是span标签,但是如果我尝试$('.document-image-frame').toggleClass();在我想要仅切换单击图像的跨度样式时松开所有span标签的样式.

这是生成的HTML:

<span class="document-image-frame">>
    <img src="file:\\105.3.2.2\upload\\10007\Desert.jpg" alt="docImg" />
</span>
Run Code Online (Sandbox Code Playgroud)

javascript css jquery razor asp.net-mvc-3

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

在Ruby方法中使用lambda

我刚刚开始,Ruby我遇到了一些我认为对语言很重要的问题所以我不想只是通过它.我真的很感激答案,其中不仅包括一个工作实例,还包括至少简要解释我哪里出错了.

所以第一步骤由具有此方法:

def filter (arr)
    arr.each do |e|
        puts e if e % 2 != 0
    end
end 

filter [1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

而预期的结果是:

1 3 5 [0.2秒内完成]

其次我试过这个:

def filter (arr)
    arr.each do |e|
        puts e if yield(e)
    end
end 

filter ([1, 2, 3, 4, 5, 6]) { |n| n.odd? }
Run Code Online (Sandbox Code Playgroud)

我得到了同样的结果:

1 3 5 [0.2秒内完成]

第三,我想用lambda做到这一点.最终我想要filter像这样调用方法filter([1, 2, 3, 4, 5, 6], &is_odd).但是,由于我仍然无法弄明白,我目前仍然坚持这个:

is_odd = lambda { …
Run Code Online (Sandbox Code Playgroud)

ruby

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