PDA

View Full Version : سوال: تبدیل کدهای ado.net به linq در SignalR



Iran58
یک شنبه 16 اردیبهشت 1397, 16:03 عصر
سلام
من کدهای زیر را نوشته ام

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using SignalR_Datbase.Hubs;


namespace SignalR_Datbase.Models
{
public class NotificationService
{
private static readonly string connString =
@"data source=test;initial catalog=InventoryManagement;integrated security=True;MultipleActiveResultSets=True;";


internal static SqlCommand commend = null;
internal static SqlDependency dependency = null;


/// <summary>
/// Get the Notifications.
/// </summary>
/// <returns></returns>
public static string GetNotification()
{
try
{
var message=new List<tbl_Notifications>();
using (var connection=new SqlConnection(connString))
{
connection.Open();
using (commend=new SqlCommand(@"select [NotificationId], [Status], [Message], [ExtraColumn] from [dbo].[tbl_Notifications] Where [Status]=2", connection))
{
commend.Notification = null;
if (dependency==null)
{
dependency=new SqlDependency(commend);
dependency.OnChange+=new OnChangeEventHandler(dependency_OnChange);
}


if (connection.State==ConnectionState.Closed)
connection.Open();


var reader = commend.ExecuteReader();
while (reader.Read())
{
message.Add(item:new tbl_Notifications
{
NotificationId = (int)reader["NotificationId"],
Status = reader["Status"]!=DBNull.Value?(string)reader["Status"]:"",
Message = reader["Message"] != DBNull.Value ? (string)reader["Message"] : "",
ExtraColumn = reader["ExtraColumn"] != DBNull.Value ? (string)reader["ExtraColumn"] : "",
});
}
}
}


var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(message);
return json;
}
catch (Exception ex)
{
return null;
}
}


private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (dependency!=null)
{
dependency.OnChange -= dependency_OnChange;
dependency = null;
}


if (e.Type==SqlNotificationType.Change)
{
MyNewHub.Send();
}
}
}
}

آیا می توانم این کدها را تبدیل به linq کنم یا باید بهمین صورت Ado.net بنویسم و معادل این کدها به linq چه می باشد
باتشکر