Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The tblMagInfo table contains three fields. The Code and Cost fields are numeric

ID: 671514 • Letter: T

Question

The tblMagInfo table contains three fields. The Code and Cost fields are numeric. The Magazine field contains text. The dataset's name is MagsDataSet.

Write a LINQ statement that arranges the records in descending order by the Cost field.

Write a LINQ statement that selects records having a code of 9.

Write a LINQ statement that selects records having a cost of $3 or more.

Write a LINQ statement that selects the Daily Food Guide magazine.

Write a LINQ statement that selects magazines whose name begins with the letter G (in either uppercase or lowercase).

Write a LINQ statement that calculates the average cost of a magazine.

Explanation / Answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class LINQuery : System.Web.UI.Page
{
DataTable tblMagInfo=new DataTable();
DataTable tblResult = new DataTable();
DataColumn dc;
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dc = new DataColumn("Code");
tblMagInfo.Columns.Add(dc);
dc = new DataColumn("Cost", typeof(int));
tblMagInfo.Columns.Add(dc);
dc = new DataColumn("Magazine");
tblMagInfo.Columns.Add(dc);

dr = tblMagInfo.NewRow();
dr["Code"] = 1;
dr["Cost"] = 3;
dr["Magazine"] = "Daily Food Guide";
tblMagInfo.Rows.Add(dr);
dr = tblMagInfo.NewRow();
dr["Code"] = 2;
dr["Cost"] = 1;
dr["Magazine"] = "Digital World";
tblMagInfo.Rows.Add(dr);
dr = tblMagInfo.NewRow();
dr["Code"] = 4;
dr["Cost"] = 4;
dr["Magazine"] = "Genuis Magazine";
tblMagInfo.Rows.Add(dr);
dr = tblMagInfo.NewRow();
dr["Code"] = 6;
dr["Cost"] = 5;
dr["Magazine"] = "George Ideas";
tblMagInfo.Rows.Add(dr);
dr = tblMagInfo.NewRow();
dr["Code"] = 7;
dr["Cost"] = 2;
dr["Magazine"] = "Computer World";
tblMagInfo.Rows.Add(dr);
dr = tblMagInfo.NewRow();
dr["Code"] = 9;
dr["Cost"] = 1;
dr["Magazine"] = "Daily Motion";
tblMagInfo.Rows.Add(dr);

//1. LINQ statement that arranges the records in descending order by the Cost field
var Rows = (from row in tblMagInfo.AsEnumerable()
orderby row["Cost"] descending
select row);
tblResult = Rows.AsDataView().ToTable();
tblResult.Rows.Clear();

//2. LINQ statement that selects records having a code of 9
var Rows1 = (from row in tblMagInfo.AsEnumerable()
where row.Field<string>("Code").ToString().Contains("9")
select row);//.Count();

tblResult = Rows1.AsDataView().ToTable();
tblResult.Rows.Clear();

//3. LINQ statement that selects records having a cost of $3 or more
var Rows2 = tblMagInfo.AsEnumerable()
.Where(x => x["Cost"] != DBNull.Value)
.Count(x => x.Field<int>("Cost") > 3);

//4. LINQ statement that selects the Daily Food Guide magazine.
var Rows3 = (from row in tblMagInfo.AsEnumerable()
where row.Field<string>("Magazine").Equals("Daily Food Guide")
select row);

tblResult = Rows3.AsDataView().ToTable();
tblResult.Rows.Clear();

// 5. LINQ statement that selects magazines whose name begins with the letter G (in either uppercase or lowercase)
var Rows4 = (from row in tblMagInfo.AsEnumerable()
where row.Field<string>("Magazine").ToString().Contains("G")
select row);
tblResult = Rows3.AsDataView().ToTable();
tblResult.Rows.Clear();

//6. LINQ statement that calculates the average cost of a magazine.
var Rows5 = tblMagInfo.AsEnumerable()
.Where(x => x["Cost"]!= DBNull.Value)
.Average(x => x.Field<int>("Cost"));


}
}
}

//Note: i created a datatable named it as tblMagInfo , fill data with temporary data ,

written linq to get your records back as data table.

If any issues please reply to me.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote