如何使用,如果内部条件的Linq [英] How to use if condition inside Linq

查看:106
本文介绍了如何使用,如果内部条件的Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发它用于搜索和绑定的DataGrid的应用程序。
哪些用户需要做的是输入文字的一部分,并点击搜索按钮,意味着它应该取从DB其中有像文本框的值的数据,并绑定到数据网格。

I'm Developing an application which is used for Search and Bind the DataGrid. What the user will do is enter the part of the text and hit search button means it should Fetch the data from DB which have like textbox value and bind to datagrid.

但我的要求是: - 在我的表,我有3个字段即标识,ChargeName和IorE.and在我的DataGrid中我有一个ID,收入和笔开支列。我需要的是作为例如:
如果我喜欢服务文本框中输入充电名称并点击搜索意味着如果它应该在 chargeName领域一旦发现是否应该检查搜索的名称 IorE领域如果值为,然后在ChargeName应该绑定为收入列 DataGrid中,如果是的E,然后ChargeName应绑定到< STRONG> Expence列 DataGrid中。我使用LINQ,我不知道如何在LINQ使用这种条件

But my requirement is:- In my table i have 3 fields namely ID,ChargeName and IorE.and in my datagrid i have columns like id ,Income and Expences. What i need is for eg: if i enter the charge name in textbox like "Service" and hit search means if it should search for the name in the chargeName field once it found if should check in IorE field if value is "I" then the ChargeName should bind to Income Column in Datagrid and if it is "E" then ChargeName should bind to Expence Column in Datagrid. I'm Using LinQ and i dont know how to use such condition in LINQ

下面是我的code去。

Here is my code goes.

using (LQMasterChargeDataContext DB = new LQMasterChargeDataContext())
{
    var Result = from C in DB.TB_MasterCharges
                 where C.mCHR_NUIsActive == 1 && SqlMethods.Like( C.mCHR_VCName,TxtChargeName.Text.Trim())
                 select new { C.mCHR_NUPKId,
                              Income = C.mCHR_VCName
                            };

    dgSailing.DataSource = Result;
    dgSailing.DataBind();
    int Count = Result.Count();
    if (Count == 0)
    {
        LBLEXcepceValue.BackColor = System.Drawing.Color.LightPink;
        LBLEXcepceValue.Font.Bold = true;
        LBLEXcepceValue.Font.Name = "Times New Roman";
        LBLEXcepceValue.Font.Size = 10;
        LBLEXcepceValue.Text = "NO VESSEL FOUND ! ! !";
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('No Vessel found');", true);
    }

和我的aspx页面,如:

and my aspx page is like:

<asp:UpdatePanel ID="UPChargesGrid" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DataGrid ID="dgDestination" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
                          BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small"
                          AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3" align="center"
                           Width="700px" OnItemCommand="dgDestination_Select">
            <Columns>
                <asp:BoundColumn DataField="ID" HeaderText="mFDD_mFRD_NUPKId" Visible="False">
                </asp:BoundColumn>
                <asp:BoundColumn DataField="Income" HeaderText="Income"></asp:BoundColumn>
                    <TemplateColumn>
                        <ItemTemplate>
                            <asp:Label ID="LBLEXcepceValue" runat="server" Text='<%# Eval("Expence").ToString().Substring(0,5) %>' ></asp:Label>
                         </ItemTemplate>
                     </TemplateColumn>
                     <asp:TemplateColumn HeaderText="SAVE">
                         <ItemTemplate>
                             <asp:ImageButton runat="server" ID="IMGBTNSave" ImageUrl="~/AppImages/Save.gif" ToolTip="Save"  CommandName="Save" 
                                   AlternateText="Save" />
                         </ItemTemplate>
                     </asp:TemplateColumn>
                </Columns>
        <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
      </asp:DataGrid>
    </contenttemplate>
</asp:UpdatePanel>    

能否任何的帮助我解决这个issue.Thanks提前。

Can any on help me to solve this issue.Thanks in advance.

我现在有界DataGrid中我要的是如果该值为null,则一个用户控件应该显示和保存按钮(在数据网格)应Enable.Please帮我做that.Here是我需要重新present通过图像

I bounded the DataGrid now what i want is if the value is null then one user control should display and save button(in datagrid) should be Enable.Please Help me to do that.Here is my need represent via image

结果

推荐答案

您可以改变这样的查询

var Result = from C in DB.TB_MasterCharges
                 where C.mCHR_NUIsActive == 1 && SqlMethods.Like( C.mCHR_VCName,TxtChargeName.Text.Trim())
                 select new { C.mCHR_NUPKId,
                              Income = C.IorE == "I" ? C.mCHR_VCName : string.Empty,
                              Expense = C.IorE == "E" ? C.mCHR_VCName : string.Empty
                            };

然后就可以使用绑定列的费用像下面

Then you can use a bound column for expense like below

<asp:BoundColumn DataField="Expense" HeaderText="Expense"></asp:BoundColumn>

更新
我希望我正确地理解你的问题。您可以使用code像下面切换的控件的可见性。

UPDATE I hope I understood your problem correctly. You can toggle the visibility of controls using the code like below

<TemplateColumn>
   <ItemTemplate>
       <asp:Label ID="LBLEXcepceValue" runat="server" Text='<%# Eval("Expence") %>' 
       Visible='<%# Eval("Expence") != null && Eval("Expence") != string.Empty %>' 
       ></asp:Label>
       <asp:TextBox ID="textBoxExpenses" runat="server" Visible='<%# Eval("Expence") == null || Eval("Expence") == string.Empty %>'></asp:TextBox>
   </ItemTemplate>
</TemplateColumn>
<asp:TemplateColumn HeaderText="SAVE">
   <ItemTemplate>
      <asp:ImageButton runat="server" ID="IMGBTNSave" ImageUrl="~/AppImages/Save.gif" ToolTip="Save"  CommandName="Save" 
      Visible='<%# Eval("Expence") == null || Eval("Expence") == string.Empty %>
      AlternateText="Save" />
   </ItemTemplate>
</asp:TemplateColumn>

这篇关于如何使用,如果内部条件的Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆