get model from List of a model [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there a way to get the specific items from the list of a model without using a foreach of the list? like if you return a List<model> holding a model with a few differnt items to specifically call those item.

If you want a list of a specific item that's within your list then you can use LINQ to return those items, for example:
var customerNames = customersList.Select(customer => customer.Name);
This would give you a string collection with all of your customer names equal to the amount that was in your original list.
Or if you would like to retrieve a number of different items from your model list then you may want to look into creating a class specifically for stripping out those items, for example:
public class CustomerContactDetails
{
public string Address { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
}
...
var contactDetails = customersList.Select(customer => new CustomerContactDetails { Address = customer.Address, Email = customer.Email, Telephone = customer.Telephone });

You can use C# LambdaExpression, like this:
var yourModel = modelList.Where(x => x.Id = 12).Select(x).First();

Related

.NET MVC var in controller is returning incorrect results [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
What the heck am I missing here. I'm having an issue adding variables to a MVC Model:
var clientnumber returns the correct value (ex. 123)
var companyname returns the correct value (ex. foo)
var blah returns "System.Web.Mvc.ContentResult" for both the clientnumber
and companyname
CONTROLLER:
var clientnumber = GetClientNumber().ToString();
var companyname = GetCompanyName().ToString();
var blah = new UserExperience { client_number = clientnumber, us_name =
companyname };
MYSQL.UserExperiences.Add(blah);
MYSQL.SaveChanges();
MODEL:
`public partial class UserExperience
{
public int ID { get; set; }
public string client_number { get; set; }
public string us_name { get; set; }
}`
Your methods GetClientNumber and GetCompanyName aren't returning what you think they're returning. Your biggest clue to this is the use of .ToString(). After all, if (as you assume) they are returning string values then why would you need to call .ToString() on them?
They're returning System.Web.Mvc.ContentResult objects. And the default string representation of an object is its class name.
We can't see those methods or anything about them, but it seems like they're not the methods you want to use here. They're likely controller actions. You don't want a ContentResult, you want a string value. Instead of calling other MVC controller actions, invoke whatever logic you need to get the value that those actions are also getting.

How can I create and return object in return statement? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to create and return an object in this return call:
var Zones = _ZoneService.GetZones();
var userZones = _ZoneService.GetUserZones(user);
return Ok(//Here I need to return both Zones and userZones);
How can I create new on the go object to return these two.
Thanks...
What about Anonymous Types? Something like this:
return Ok(new { Zones, userZones });
I think that the best way to go is to creat a list, add the object to your list, and return the list. This way is also easyer if you need to work with these object.
To create a list, first we need to code a propper class:
public class Item {
public int Id { get; set; }
public Object MyObj { get; set; }
}
Now we can create and populate the list:
List<Item> items = new List<Item>()
{
new Item{ Id=1, MyObj= Zones},
new Item{ Id=2, MyObj= userZones}
}
Now you can return your list using: return items
You could also use anout parameter, like:
public Zones GetZones(..., out Zones userZones)
{
uzerZones = _ZoneService.GetUserZones(user);
return _ZoneService.GetZones();
}

filtering a list by an object having list of string properties [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Class
public class AlldataPoints
{
public int phase{ get; set; }
public string recr{ get; set; }
public string study{ get; set; }
}
public class filter
{
public List<string> Phase;
public List<string> recr;
public List<string> study;
}
I have a list of above class say Alldatapoints 'lst' and an object of class filter 'obj'.
I want to filter only those rows of 'lst' that in which values of phases in 'obj' matches with 'lst' 'phases' and values of 'recr' in 'obj' matches 'recr' in 'lst' 'recr'. i want to use entity expression.
Use Contains on the list of filter arguments:
filter obj;
IEnumerable<AlldataPoints> lst;
var result = lst.Where(item => obj.Phase.Contains(item.phase) && obj.recr.Contains(item.recr));
Here you go:
public List<AlldataPoints> FilterPoinst(List<AlldataPoints> points, filter filter)
{
return points.Where(x => filter.Phase.Contains(x.phase)
&& filter.recr.Contains(x.recr)
&& filter.study.Contains(x.study)).ToList();
}
If you need to match ony one property then just replace && with || operator:
In your code just call this function:
var filter = new filter(); //init your filter
List<AlldataPoints> points = new List<AlldataPoints>(); // Here you should get your points
var filteredPoints = FilterPoinst(points, yourFilter); //you get your filtered points.
If this is a Linq-to-Entities query against your DbContext, you can still use Contains() to check if each value is in each corresponding list.
Once awkward thing is that you need to convert phase to a string, as it is an int and the list is a List<string>, but you cannot just use ToString(), you have to use SqlFunctions.StringConvert() - see this answer.
dbContext.AllDataPoints.Where(adp =>
filter.Phase.Contains(SqlFunctions.StringConvert((double)adp.phase).Trim())
&& filter.recr.Contains(adp.recr)
// && filter.study.Contains(adp.study)
)

How to select two set from collection in linq [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a collection defined by:
public class CompanyModel
{
public int compnSN { get; set; }
public string compnName { get; set; }
public string compnAddress { get; set; }
}
How can I get out two set compnName and compnAddress together so that I can use each of that pair value one by one in a foreach loop ?
I tried:
var pairs = from companies
select new {name = companies.compnName, addr = companies.compnAddress}
foreach (var item in pairs)
{
//do some things with item.name;
//do some things with item.addr;
}
I know this idea can be executed but is my grammar wrong somewhere?
Try this one:
var result = companies.Select(x => new
{
Name = x.compnName,
Address = x.compnAddress
});
or this one:
var result = from company in companies
select new
{
Name = x.compnName,
Address = x.compnAddress
};
Then as you already pointed out:
foreach(var company in result)
{
// access the name like company.Name and do what you want.
// access the address like company.Address and do what you want.
}
UPDATE
The code you have posted has some grammatical errors. So despite the fact that your logic is correct, this piece of code will not be compiled.
Specifically, in the following piece of code:
var pairs = from companies
select new {name = companies.compnName, addr = companies.compnAddress}
you try to declare a local variable called companies that should belong in a collection.
Hence we should write this like below:
var pairs = from item in items
Now item is a local variable that refers to the random element in the collections called items. Then as you already have writen you will declare an anonymous type in the select clause:
var pairs = from item in items
select new
{
PropertyA = item.PropertyA
PropertyB = item.PropertyB
};

Filter Object List & Bind to Datagrid [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an object (please see code below) which contains a list. This list is currently binded to a datagrid in my WPF application.
What I would like to do is to have two datagrids bounded to my object List. I want the top grid to show all the items that have a Status of true. The bottom datagrid would show all the items that have a Status of false. I want both datagrids to be bound to the same object List. Can someone please send me some example code of how to do this?
C# Code
class OrderBlocks
{
public string setting;
public List<Order> Orders;
}
class Order
{
public double Amount;
public int Name;
public bool Status;
}
XAML Code
<DataGrid DataContext="{Binding OrderBlock}"
Name="dataGridOrders"
ItemsSource="{Binding Orders}"/>
You could create 2 properties in your OrderBlocks class
public List<Order> OrdersWithStatusTrue
{
get { return Orders.Where(x => x.Status); }
}
public List<Order> OrdersWithStatusFalse
{
get { return Orders.Where(x => !x.Status); }
}
and bind the first grid to the first property the second one to other.

Categories

Resources