@using Syncfusion.EJ2.Blazor
@using Syncfusion.EJ2.Blazor.Schedule
@using Syncfusion.EJ2.Blazor.Data
<EjsSchedule ID="schedule" Height="550px" SelectedDate="new DateTime(2018, 5, 10)">
<ScheduleEventSettings>
<EjsDataManager Url="http://localhost:25255/odata/EventDatas"Adaptor="Adaptors.ODataV4Adaptor"></EjsDataManager>
</ScheduleEventSettings>
</EjsSchedule> |
public class EventDatasController : ODataController
{
private ScheduleDataEntities1 db = new ScheduleDataEntities1();
// GET: odata/EventDatas
[EnableQuery]
[AcceptVerbs("GET")]
public IQueryable<EventData> GetEventDatas()
{
return db.EventDatas;
}
// GET: odata/EventDatas(5)
[EnableQuery]
[AcceptVerbs("GET")]
public IQueryable<EventData> GetEventDatas(string StartDate, string EndDate)
{
DateTime start = DateTime.Parse(StartDate);
DateTime end = DateTime.Parse(EndDate);
return db.EventDatas.Where(evt => evt.StartTime >= start && evt.EndTime <= end);
}
// POST: odata/EventDatas
[AcceptVerbs("POST", "OPTIONS")]
public void Post([FromBody]CrudData eventData)
{
EventData insertData = new EventData();
insertData.Id = (db.EventDatas.ToList().Count > 0 ? db.EventDatas.ToList().Max(p => p.Id) : 1) + 1;
insertData.StartTime = Convert.ToDateTime(eventData.StartTime).ToLocalTime();
insertData.EndTime = Convert.ToDateTime(eventData.EndTime).ToLocalTime();
insertData.Subject = eventData.Subject;
insertData.IsAllDay = eventData.IsAllDay;
insertData.Location = eventData.Location;
insertData.Description = eventData.Description;
insertData.RecurrenceRule = eventData.RecurrenceRule;
insertData.RecurrenceID = eventData.RecurrenceID;
insertData.RecurrenceException = eventData.RecurrenceException;
insertData.StartTimezone = eventData.StartTimezone;
insertData.EndTimezone = eventData.EndTimezone;
db.EventDatas.Add(insertData);
db.SaveChanges();
}
// PATCH: odata/EventDatas(5)
[AcceptVerbs("PATCH", "MERGE", "OPTIONS")]
public void Patch([FromBody]CrudData eventData)
{
EventData updateData = db.EventDatas.Find(Convert.ToInt32(eventData.Id));
if (updateData != null)
{
updateData.StartTime = Convert.ToDateTime(eventData.StartTime).ToLocalTime();
updateData.EndTime = Convert.ToDateTime(eventData.EndTime).ToLocalTime();
updateData.Subject = eventData.Subject;
updateData.IsAllDay = eventData.IsAllDay;
updateData.Location = eventData.Location;
updateData.Description = eventData.Description;
updateData.RecurrenceRule = eventData.RecurrenceRule;
updateData.RecurrenceID = eventData.RecurrenceID;
updateData.RecurrenceException = eventData.RecurrenceException;
updateData.StartTimezone = eventData.StartTimezone;
updateData.EndTimezone = eventData.EndTimezone;
db.SaveChanges();
}
}
// DELETE: odata/EventDatas(5)
[AcceptVerbs("DELETE", "OPTIONS")]
public void Delete([FromODataUri]int key)
{
EventData removeData = db.EventDatas.Find(key);
if (removeData != null)
{
db.EventDatas.Remove(removeData);
db.SaveChanges();
}
}
} |
<EjsGrid id="Grid" @ref="grid" AllowPaging="true" DataSource="@griddata">
<GridColumns>
……………………………………………….
</GridColumns>
</EjsGrid>
……………………………………………….
@code{
EjsGrid grid;
public List<Orders> griddata = new List<Orders>();
public DataTable dt = new DataTable("Order");
protected override void OnInit()
{
//use your connection string here
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='D:\Blazor Samples\SqlServer\SqlServer\SqlServer\Data\NORTHWND.MDF';Integrated Security=True;Connect Timeout=30";
SqlConnection myConnection = new SqlConnection(connectionString);
dt = new DataTable("Order");
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "select * from Orders";
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
da.Fill(dt);
dataBind();
}
protected void dataBind()
{
foreach (DataRow row in dt.Rows)
{
griddata.Add(new Orders
{
OrderID = Convert.ToInt32(row["OrderID"]),
CustomerID = row["CustomerID"].ToString(),
EmployeeID = Convert.ToInt32(row["EmployeeID"]),
……………………………………………………………………
});
}
}
public class Orders
{
public Orders()
{
}
public Orders(int orderId, string customerId, int empId, double freight, string shipName, string shipCountry)
{
………………………………………………………
}
public int OrderID { get; set; }
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public double Freight { get; set; }
public string ShipName { get; set; }
public string ShipCountry { get; set; }
}
} |
<EjsGrid @ref="@grid" AllowPaging="true">
<EjsDataManager Url="/api/Default" Adaptor="Adaptors.UrlAdaptor"></EjsDataManager>
<GridColumns>
<GridColumn Field="OrderID" HeaderText="Employee ID" ISPrimaryKey="true" TextAlign="@TextAlign.Right" Width="90"></GridColumn>
<GridColumn Field="CustomerID" HeaderText="First Name" Width="90"></GridColumn>
<GridColumn Field="EmployeeID" HeaderText="Employee ID" Width="90"></GridColumn>
<GridColumn Field="Freight" HeaderText="Freight" Width="90"></GridColumn>
</GridColumns>
</EjsGrid> |
[HttpPost]
public object Post([FromBody]DataManagerRequest dm)
{
IEnumerable DataSource = db.GetAllOrders().ToList();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = DataOperations.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<Order>().Count();
if (dm.Skip != 0)
{
DataSource = DataOperations.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = DataOperations.PerformTake(DataSource, dm.Take);
}
return new { result = DataSource, count = count };
} |
<EjsGrid id="Grid" @ref="@grid" AllowPaging="true" AllowSorting="true" AllowFiltering="true" toolbar="@(new List<string>() {"Add", "Edit", "Delete", "Cancel", "Update" })">
<EjsDataManager Url=" http://localhost:51972/api/Default" Adaptor="Adaptors.UrlAdaptor"></EjsDataManager> <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Normal"></GridEditSettings>
<GridColumns>
…………………………………………..
</GridColumns>
</EjsGrid> |
System.InvalidOperationException: The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
How to use this efficiently using IQueryable with large database tables ??
System.InvalidOperationException: The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Above error occurs when search do a post to web api.
Blazor Code
<EjsGrid TValue="SystemUserList" AllowPaging="true" Toolbar="@(new List<string>() { "Search"})">
<GridEvents TValue="SystemUserList" OnActionFailure="@ActionFailure" CommandClicked="@OnCommandClicked" RowDataBound="RowBound" ></GridEvents>
<EjsDataManager Url="https://localhost:44365/api/systemuser" Adaptor="Adaptors.UrlAdaptor"></EjsDataManager>
<GridColumns>
<GridColumn Field=@nameof(SystemUserList.Id) HeaderText="ID" Width="50"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.Username) HeaderText="Username" Width="100"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.DisplayName) HeaderText="DisplayName" Width="100"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.Email) HeaderText="Email" Width="150"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.Mobile) HeaderText="Mobile" Width="80"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.LastLoginDate) HeaderText="LastLoginDate" Width="100" Format="yMd"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.LastPasswordChanged) HeaderText="LastPasswordChanged" Width="100" Format="yMd"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.SystemIp) HeaderText="SystemIp" Width="100"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.IsAdmin) HeaderText="IsAdmin" Width="50" DisplayAsCheckBox="true"></GridColumn>
<GridColumn Field=@nameof(SystemUserList.IsDisabled) HeaderText="IsDisabled" Width="50" DisplayAsCheckBox="true"></GridColumn>
<GridColumn TextAlign="TextAlign.Right" Width="50">
<GridCommandColumns>
<GridCommandColumn ButtonOption="@(new CommandButtonOptions() { Content = "Edit", CssClass = "btn btn-square btn-block btn-warning" })"></GridCommandColumn>
</GridCommandColumns>
</GridColumn>
<GridColumn TextAlign="TextAlign.Right" Width="60">
<GridCommandColumns>
<GridCommandColumn ButtonOption="@(new CommandButtonOptions() { Content = "Delete", CssClass = "btn btn-square btn-block btn-danger" })"></GridCommandColumn>
</GridCommandColumns>
</GridColumn>
</GridColumns>
</EjsGrid>
Web API Code
[HttpPost]
public object Post([FromBody] DataManagerRequest dm)
{
IEnumerable dataSource = _db.SystemUser.Select(x => new SystemUserList
{
Id = x.Id,
Username = x.Username,
DisplayName = x.DisplayName,
Email = x.Email,
Mobile = x.Mobile,
LastLoginDate = x.LastLoginDate,
LastPasswordChanged = x.LastPasswordChanged,
SystemIp = x.SystemIp,
IsAdmin = x.IsAdmin,
IsDisabled = x.IsDisabled,
SystemName = x.SystemName,
});
if (dm.Search != null && dm.Search.Count > 0)
{
dataSource = DataOperations.PerformSearching(dataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
dataSource = DataOperations.PerformSorting(dataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
dataSource = DataOperations.PerformFiltering(dataSource, dm.Where, dm.Where[0].Operator);
}
int count = dataSource.Cast<SystemUserList>().Count();
if (dm.Skip != 0)
{
dataSource = DataOperations.PerformSkip(dataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
dataSource = DataOperations.PerformTake(dataSource, dm.Take);
}
return new { result = dataSource, count = count };
}
Not sure how I can use CustomAdaptor on above code. I am using .Net Core 3 with Syncfusion 17.3.0.18.
[HttpPost]
public object Post([FromBody] DataManagerRequest dm)
{
IEnumerable dataSource = _db.SystemUser.Select(x => new SystemUserList
{
Id = x.Id,
var dataSource = _db.SystemUser;
... etc.
Modify the
<EjsGrid TValue="SystemUserList" AllowPaging="true" Toolbar="@(new List<string>() { "Search"})"> <GridEvents TValue="SystemUserList" OnActionFailure="@ActionFailure" CommandClicked="@OnCommandClicked" RowDataBound="RowBound"></GridEvents> <EjsDataManager Url="https://localhost:44365/api/systemuser" Adaptor="Adaptors.UrlAdaptor"></EjsDataManager> <GridColumns> <GridColumn Field=@nameof(SystemUserList.Id) HeaderText="ID" Width="50"></GridColumn> <GridColumn Field=@nameof(SystemUserList.Username) HeaderText="Username" Width="100"></GridColumn> <GridColumn Field=@nameof(SystemUserList.DisplayName) HeaderText="DisplayName" Width="100"></GridColumn> <GridColumn Field=@nameof(SystemUserList.Email) HeaderText="Email" Width="150"></GridColumn> <GridColumn Field=@nameof(SystemUserList.Mobile) HeaderText="Mobile" Width="80"></GridColumn> <GridColumn Field=@nameof(SystemUserList.LastLoginDate) HeaderText="LastLoginDate" Width="100" Format="yMd"></GridColumn> <GridColumn Field=@nameof(SystemUserList.LastPasswordChanged) HeaderText="LastPasswordChanged" Width="100" Format="yMd"></GridColumn> <GridColumn Field=@nameof(SystemUserList.SystemIp) HeaderText="SystemIp" Width="100"></GridColumn> <GridColumn Field=@nameof(SystemUserList.IsAdmin) HeaderText="IsAdmin" Width="50" DisplayAsCheckBox="true"></GridColumn> <GridColumn Field=@nameof(SystemUserList.IsDisabled) HeaderText="IsDisabled" Width="50" DisplayAsCheckBox="true"></GridColumn> <GridColumn TextAlign="TextAlign.Right" Width="50"> <GridCommandColumns> <GridCommandColumn ButtonOption="@(new CommandButtonOptions() { Content = "Edit", CssClass = "btn btn-square btn-block btn-warning" })"></GridCommandColumn> </GridCommandColumns> </GridColumn> <GridColumn TextAlign="TextAlign.Right" Width="60"> <GridCommandColumns> <GridCommandColumn ButtonOption="@(new CommandButtonOptions() { Content = "Delete", CssClass = "btn btn-square btn-block btn-danger" })"></GridCommandColumn> </GridCommandColumns> </GridColumn> </GridColumns> </EjsGrid>
[HttpPost] public object Post([FromBody] DataManagerRequest dm) { var dataSource = _db.SystemUser.Select(x => new SystemUserList { Id = x.Id, Username = x.Username, DisplayName = x.DisplayName, Email = x.Email, Mobile = x.Mobile, LastLoginDate = x.LastLoginDate, LastPasswordChanged = x.LastPasswordChanged, SystemIp = x.SystemIp, IsAdmin = x.IsAdmin, IsDisabled = x.IsDisabled, SystemName = x.SystemName, }); if (dm.Search != null && dm.Search.Count > 0) { dataSource = DataOperations.PerformSearching(dataSource, dm.Search); //Search } if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting { dataSource = DataOperations.PerformSorting(dataSource, dm.Sorted); } if (dm.Where != null && dm.Where.Count > 0) //Filtering { dataSource = DataOperations.PerformFiltering(dataSource, dm.Where, dm.Where[0].Operator); } int count = dataSource.Cast<SystemUserList>().Count(); if (dm.Skip != 0) { dataSource = DataOperations.PerformSkip(dataSource, dm.Skip); //Paging } if (dm.Take != 0) { dataSource = DataOperations.PerformTake(dataSource, dm.Take); } return new { result = dataSource, count = count };
public Object Post([FromBody]DataManagerRequest dm)
{
// Here we have converted dataSource as list to resolve the problem
IEnumerable DataSource = db.Orders.Select(x => new Order
{
OrderID = x.OrderID,
EmployeeID = x.EmployeeID,
CustomerID = x.CustomerID
}).ToList();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = DataOperations.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<Order>().Count();
if (dm.Skip != 0)
{
DataSource = DataOperations.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = DataOperations.PerformTake(DataSource, dm.Take);
}
return new { result = DataSource, count = count };
}
|