MVC - Ordered List - c#

I am trying to order the following list by descending but I can't seem to figure out how it is done:
var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name
}).ToList();
ViewBag.Customers = new MultiSelectList(cust, "id", "Name");
This is what I have already tried:
var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name.OrderByDescending();
}).ToList();
ViewBag.Customers = new MultiSelectList(cust, "id", "Name");
This is how the list is displayed on my view:
#Html.DropDownList("id", (MultiSelectList)ViewBag.Customers, new { #class = "form-control", id = "lstCustomer" })
Note: I am trying to sort the list in alphabetical order

var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name
}).OrderByDescending(c=>c.Name).ToList();
Or
var cust = item.Profiles.OrderByDescending(a=>a.Name).Select(c => new
{
id = c.CustId,
Name = c.Name
}).ToList();

Using Linq:
var cust = item.Profiles.Select(c => new
{
id = c.CustId,
Name = c.Name
}).OrderByDescending(c => c.Name).ToList();
Or More Elegant (Query Syntax):
var cust = (from c in item.Profiles
orderby c.Name
select new
{
id = c.CustId,
Name = c.Name
}).ToList();

To do the same thing with query syntax:
var cust = (from c in item.Profiles
orderby c.Name descending
select new
{
id = c.CustId,
Name = c.Name
}
).ToList();

Related

ASP.NET MVC - Get Id from a dropdownlist

I'm new in ASP.NET MVC and i have a problem. I needed to get a string "Category" from a dropdownlist, that was my code:
var CList = new List<string>();
StreetFooder sf = db.StreetFooders.Single(s => s.User.UserName == this.User.Identity.Name);
var Cqry = from d in db.Categories
where !d.IsDeleted && d.StreetFooder.Id == sf.Id
orderby d.Name
select d.Name;
CList.AddRange(Cqry.Distinct());
ViewBag.CategoryList = new SelectList(CList);
And the view:
<div class="col-md-10">
#Html.DropDownListFor(model => model.Category, (SelectList)ViewBag.CategoryList, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Category, "", new { #class = "text-danger" })
</div>
And that worked for me. After that, i realized i made a mistake and changed my string Category attribute in:
public virtual Categories Type { get; set; }
Now i want to save on my db the ID instead of string, but still want to see my string when select from dropdown list...but don't know how.
Use SelectListItem to create the SelectList, there you can specify Value = your id and Text = display text
https://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=vs.118).aspx
var CList = new List<SelectListItem>();
StreetFooder sf = db.StreetFooders.Single(s => s.User.UserName == this.User.Identity.Name);
var Cqry = from d in db.Categories
where !d.IsDeleted && d.StreetFooder.Id == sf.Id
orderby d.Name
select d;
CList.AddRange(Cqry.Distinct().Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = d.Name
}));
ViewBag.CategoryList = new SelectList(CList);
You can even omit using the SelectList and simply pass a List<SelectListItem> if I recall correctly.
Try this:
var cqry = from d in db.Categories
where !d.IsDeleted && d.StreetFooder.Id == sf.Id
orderby d.Name
select new { Id = d.StreetFooder.Id, Value = d.Name};
ViewBag.CategoryList = new SelectList(cqry.Distinct(), "Id", "Value");
You need to also define Id and pass it to your DropDownList. This will initialize a new instance of the SelectList class by using the specified items for the list(cquery), the data value field(Id), and the data text field(Value) and add to you ViewBag then bind it to DropDownList.
You can use it on View like this:
#Html.DropDownList("CategoryList ", null, new { #class = "form-control" })
cqry.Distinct() will work if you have duplicates on result, which means that the Id and Value in collection are same, if you need to distinct only by name, you need to use group by.
In selectList, you can pass from which property you want to bind dropdown value and text property.
var Cqry = from d in db.Categories
where !d.IsDeleted && d.StreetFooder.Id == sf.Id
group d by new { names.Name } into nameGroup
select new { Name = nameGroup.Name, Id = nameGroup.FirstOrDefault().StreetFooder.Id};
ViewBag.CategoryList = new SelectList(Cqry,"Id","Name");
You were selecting Distinct name, so I have used here group by name.
As I've created anonymous object with property 'Id' and Name. Now you can bind it in SelectList.

Getting employees in each dept using LINQ LAMBDA

I am trying to join two tables employee and department using LINQ and trying to get results in given below image format. Can some one help how to achieve this.
how to achieve employees present in each department.
Here is my code
var EmpList = (from d in Department
join e in Employee on d.ID equals e.ID
select new
{
ID = d.ID, Name = d.Name, Location = d.location, Employess =
e.FirstName, e.LastName, e.Gender
});
The above code not fully written. I am not getting any ideas how to achieve this.
var elist = from d in db.Departments
join e in db.Employees on d.ID equals e.ID
group d by e.DepartmentId into g
select new { Details = g };
Assuming that you have a structure like this:
var depts = new[] {
new Dept { ID = 1, Name = "IT", Location = "New York" },
new Dept { ID = 2, Name = "HR", Location = "London" },
new Dept { ID = 3, Name = "Payroll", Location = "Sydney" }
};
var employees = new[] {
new Employee { ID = 1, FirstName = "Mark", DeptID = 1 },
new Employee { ID = 2, FirstName = "Steve", DeptID = 3 },
new Employee { ID = 3, FirstName = "Ben", DeptID = 1 },
new Employee { ID = 4, FirstName = "Philip", DeptID = 2 },
new Employee { ID = 5, FirstName = "Mary", DeptID = 2 },
new Employee { ID = 6, FirstName = "Valarie", DeptID = 3 },
new Employee { ID = 7, FirstName = "John", DeptID = 1 }
};
You can use LINQ Join and GroupBy to get the desired data:
var result = depts
.Join(employees.GroupBy(x => x.DeptID), dept => dept.ID, empGroup => empGroup.Key,
(dept, empGroup) => new {
Name = dept.Name,
Location = dept.Location,
Employees = empGroup.ToArray()
});
Or the same in SQL-like syntax:
var result = from dept in depts
join empGroup in (
from e in employees
group e by e.DeptID into g
select g
) on dept.ID equals empGroup.Key
select new {
Name = dept.Name,
Location = dept.Location,
Employees = empGroup.ToArray()
};
What does this actually do?
Employees are grouped by DeptID.
Departments are joined with this grouping and result into a collection of anonymous objects of desired format. Of course, you can use a strongly-typed class.
public class Meta
{
}
public partial class Empolyees
{
public string depart
{
get
{
EmpolyeeEntities db = new EmpolyeeEntities();
var empid = db.Empolyees.Select(e=>e.Department).SingleOrDefault();
var dpname = db.Department1.Where(x => x.Id == empid).Select(f => f.Department).SingleOrDefault();
return dpname.ToString();
}
}

How to add or pre-pend hard coded data with linq resultset c#

suppose below query is returning City ID and City Name but if i want to pre-pend some hard coded data with linq result then how could i do it
var cityList = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name })
.ToList();
the above query will return data this way
City ID City Name
-------- ------------
1 Bombay
2 Kolkata
3 Delhi
but i want result this way
City ID City Name
--------- -----------
0 --Select City--
1 Bombay
2 Kolkata
3 Delhi
so i want to add this data 0 as ID and --Select City-- as Name to the list which return linq query. help me with sample code. thanks
Umm... just Insert() it?
var cityList = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name })
.ToList();
// yup, this is what I would do...
cityList.Insert(0, new City{ Id = 0, Name = "--Select City--"});
solution with Enumerable.Concat
City[] prepend = new City[] {new City { Id = 0, Name = "--Select City--"}};
IEnumerable<City> cities = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name });
var citySequence = prepend.Concat(cities);
var cityList = citySequence.ToList();

Linq: When object is null an exception is thrown

I am having difficulty with a query.
string userId = User.Identity.GetUserId();
var houseViewModel = this.Data.Houses.All()
.Where(u => u.UserId == userId && u.Name == houseName)
.Select(h => new HouseViewModel
{
Id = h.Id,
Name = h.Name,
ImageUrl = h.ImageUrl,
FloorsViewModel = h.Floоrs.Where(f=>f.Id>0)
.Select(f => new FloorViewModel
{
Name = f.Name,
RoomViewModel = f.Rooms.Where(r => r.Id > 0)
.Select(r => new RoomViewModel
{
Id = r.Id,
Name = r.Name,
SensorViewModel = new SensorViewModel
{
Id = r.Sensor.Id,
CurrentTemp = r.Sensor.CurrentTemp,
},
})
})
})
.SingleOrDefault();
When he came to the room in which no sensor throws exception because Id for SensorViewModel is a non-nullable property.
SensorViewModel = new SensorViewModel
{
Id = r.Sensor.Id,
SensorViewModel = r.Sensor == null ? new SensorViewModel() : new SensorViewModel
{
Id = r.Sensor.Id,
CurrentTemp = r.Sensor.CurrentTemp,
},
next try. Simple select only rooms with sensor.
RoomViewModel = f.Rooms.Where(r => r.Id > 0 && r.Sensor != null)
.Select(r => new RoomViewModel
{
Id = r.Id,
Name = r.Name,
SensorViewModel = new SensorViewModel
{
Id = r.Sensor.Id,
CurrentTemp = r.Sensor.CurrentTemp,
},
})
last attempt, select data from EF into list and then make viewmodel
var house = this.Data.Houses.All()
.Where(u => u.UserId == userId && u.Name == houseName).ToList();
var houseViewModel = house.Select(h => new HouseViewModel
{
Id = h.Id,
Name = h.Name,
ImageUrl = h.ImageUrl,
FloorsViewModel = h.Floоrs.Where(f=>f.Id>0)
.Select(f => new FloorViewModel
{
Name = f.Name,
RoomViewModel = f.Rooms.Where(r => r.Id > 0)
.Select(r => new RoomViewModel
{
Id = r.Id,
Name = r.Name,
SensorViewModel =r.Sensor == null ? null : new SensorViewModel
{
Id = r.Sensor.Id,
CurrentTemp = r.Sensor.CurrentTemp,
},
})
})
})
.SingleOrDefault();

Is there a better LINQ Query to do this?

Lets say I have a flat list of objects, each of which has a person's name, and a single Role they're in, like so:
var People = new[] {
new PersonRole(){ Name = "Adam", Role = "R1" },
new PersonRole(){ Name = "Adam", Role = "R2" },
new PersonRole(){ Name = "Adam", Role = "R3" },
new PersonRole(){ Name = "Bob", Role = "R1" },
};
Now, is there a direct way to get this into a Dictionary<string, List<string>> based on Name and Role (obviously). I did it in two steps, like below, but I have to think there's a more direct way.
Dictionary<string, List<string>> resultLookup =
People.Select(p => p.Name).Distinct().ToDictionary(str => str, str => new List<string>());
People.ForEach(p => resultLookup[p.Name].Add(p.Role));
Thanks!
Dictionary<string, List<string>> dictionary = people.GroupBy(p => p.Name).ToDictionary(g => g.Key, g => g.Select(p => p.Role).ToList());
Untested, but I think that should work...
EDIT Tested it now, and that does in fact work.
This should do it:
var people = new[] {
new { Name = "Adam", Role = "R1" },
new { Name = "Adam", Role = "R2" },
new { Name = "Adam", Role = "R3" },
new { Name = "Bob", Role = "R1" },
};
var r = from p in people
group p by p.Name
into g
select new {
Name = g.Key,
Roles = g.Select(p => p.Role).ToList()
};
var d = r.ToDictionary(k => k.Name, e => e.Roles);
var dict = People.ToLookup(p=>p.Name, p=>p.Role)
.ToDictionary(it=>it.Key, it=>it.ToList());
var result = People.GroupBy(a => a.Name).ToDictionary(a => a.First().Name, b => b.Select(c => c.Role).ToList());
People
.GroupBy( p => p.Name )
.ToDictionary( p => p.Key, p => p.Select(pr => pr.Role).ToList() )

Categories

Resources