Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Tuesday, November 28, 2017

Linq To SQL Grouping & Joining & ( ? , : ) operators?




Grouping of Lists:


 var list = ssrsObj.GetData();

        var g=from p in list
              group p by  new {login= p.login, Name= p.Name} into grp
              select new 
              {
                 AssigneeLogin=grp.Key.login,
                 AssigneeName=grp.Key.Name,
                 IncCounts=grp.Count()
              };

Joining Two Lists:


var joinList = from p in list1
                    join q in list2
                    on p.AgentLogin.ToLower() equals q.AssigneeLogin.ToLower() into grp
                    from x in grp.DefaultIfEmpty()
                    select new  
                    {
                       //  below statement shows if value x is null replace it with other value
                      //  p == null ? 10 : p.CCounts
                      //  ?  mark says that if p == null ? then assign x == 10 where x== p.CCounts
                                                    
                        ssrslogin  = x  ==   null   ? p.AgentLogin : x.AssigneeLogin ,
                        ssrsname   = x == null ? p.AgentName : x.AssigneeName,
                        ssrscount  = x  ==  null ? 0 : x.ICounts,


                        calllogin = p == null ? x.AssigneeLogin : p.AgentLogin,
                        callname = p == null ? x.AssigneeName : p.AgentName,
                        callcount  = p == null ? 0 : p.CCounts,                                              

                    };

Sunday, September 18, 2016

FusionChart with C#,Linq,EntiyFramework,Asp .NET,Xml Best Way.



Step1:

-------------------------------------------Page Html-------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>FusionChart Test</title
    <script src="FusionChart/fusioncharts.js" type="text/javascript"></script>
    <script src="FusionChart/fusioncharts.charts.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
   
    </div>
    </form>
</body>
</html>


-------------------------------------------------------------------------------------

Step2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using InfoSoftGlobal;

public partial class FusionChartTest : System.Web.UI.Page
{
  
    Entities ctx = new Entities();

    protected void Page_Load(object sender, EventArgs e)
    {

---------------Getting the required result and save into class-----------------------

        var model = ctx.Table
           
            .Where(o=>o.DateTime.Value.Year==DateTime.Now.Year)

            .GroupBy(o => new
            {
                Month =(int) o.DateTime.Value.Month,
                Year = o.DateTime.Value.Year
            })
            .Select(g => new SomeClass
            {
                Month = g.Key.Month,
                Year = g.Key.Year,
                Value = g.Count()
            })
            .OrderByDescending(a => a.Year)
            .ThenByDescending(a => a.Month)
            .ToList();

         List<SomeClass> list=new List<SomeClass>(model);
-------------------------------------------------------------------------------------



--------------------------Forming the xml to display as chart------------------------

         StringBuilder xmlData = new StringBuilder();

         xmlData.Append("<chart caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' showValues='0' formatNumberScale='0' showBorder='1'>");
     
        foreach (var item in list)
       {
         xmlData.AppendFormat("<set label='{0}' value='{1}' />", item.Month + "," + item.Year, item.Value);
       }

        xmlData.Append ( "</chart>"); 
       
        Literal1.Text = InfoSoftGlobal.FusionCharts.RenderChart("FusionChart/Charts/line.swf", "", xmlData.ToString(), "browser_share", "640", "340", false, true);
    }
-------------------------------------------------------------------------------------
  

}

--------------------------------Custom Class to Save the Result----------------------

public class SomeClass
{
    public int Month { get; set; }
    public int Year { get; set; }
    public int Value { get; set; }
}
-------------------------------------------------------------------------------------


Secure you Asp .NET by Web.config & Global.ascx?

Add to Global.ascx protected void Application_BeginRequest(object sender,EventArgs e)     {         //to remove x frame         Resp...