C# Copy List items to Object Arrays - c#

I have a list created from a stored procedure using EF6.0
I have also created 3 classes
public class Resas
{
public string todo{ get; set; }
public string prop { get; set; }
public string Code { get; set; }
public string statusCode { get; set; }
public string checkin { get; set; }
public string checkout { get; set; }
public List<profiles> profiles { get; set; }
}
public class profiles
{
public string action { get; set; }
public string id { get; set; }
public string profileType { get; set; }
public string title { get; set; }
public string firstName { get; set; }
public string middleName { get; set; }
public string lastName { get; set; }
public List<emailAddresses> emailAdresses { get; set; }
}
public class emailAddresses
{
public string emailAddress { get; set; }
public string emailAddress2 { get; set; }
}
I am doing a for-loop in the list and I need to get certain columns and put it in the array (I will put two, to keep it simple)
myEntities db = new myEntities();
List<rev_Result> revList = new List<rev_Result>();
revList.Clear();
revList = db.rev().ToList();
for (int i = 0; i < revList.Count(); i++)
{
Resas resas = new Resas();
profiles[] profiles = new profiles[1];
resas.todo = revList[i].todo;
resas.profiles[0].lastName = revList[i].lastName;
}
I am not familiar with C# as you can see from the psedo-code above.
I cannot figure out how to feed the Resas with data and then its Profile with data and then move to the next Resas entry.
Any help appreciated.

That's fairly simple using Linq:
Resas resas = new Resas();
resas.profiles = revList
.Select(x => new profiles() { action = x.todo, lastName = x.lastName })
.ToList();
What's happening here is: You loop through every entry in revList and get your wanted data structure (that's what Select is doing). x refers to the current entry in the loop, while the stuff to the right side of the arrow is you 'output': a new instance of your profiles class with the members assigned accordingly. The result of all of this is then converted to a list (before ToList(), think of it as a recipe to create the list) and assigned to resas.profiles.
By the way, a word on conventions: Usually, in C#, you would give your classes a name that starts with a capital letter. Also, your profiles class seems to contain data of exactly one profile, so a better name might be Profile. This also makes your data structure more clear, since List<profiles> seems to be a list of lists of profiles - but that's not what it actually is, is it?
Furthermore, Members generally start with a capital letter as well, so instead of action, lastName, you'd have: Action and LastName.

You can try with Linq. This is the code that should solve your issue, but Resas class doesn't have action property:
List<Resas> ls = revList.Select(x => new Resas() {
action = x.todo,
profiles = new List<profiles>() {
new profiles { lastName = x.lastName }
}
).ToList();
If you need to use action property of inprofiles` class:
List<Resas> ls = revList.Select(x => new Resas() {
profiles = new List<profiles>() {
new profiles {
action = x.todo,
lastName = x.lastName
}
}
).ToList();

Related

unable to get list of elements in list

i have 2 model classes
public class ProductOptionRequest
{
public string Name { set; get; }
public List<ProductValuesRequest> productValues { get; set; }
}
public class ProductValuesRequest
{
public string ValueName { get; set; }
}
public class ProductOptionValue
{
public int OptionId { get; set; }
public String ValueName { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
and wrote one bs method and passing parameter value as value names. but I'm unable to get those values in a list object as productValues. May I know the solution, please.
public async Task<ReturnString> SaveProductOption(ProductOptionRequest request)
{
request.productValues = new List<ProductValuesRequest>();
foreach (ProductValuesRequest valueRequest in request.productValues)
{
ProductOptionValue res = new ProductOptionValue();
res.ValueName = valueRequest.ValueName;
object response = await productOptionValueRepository.InsertAsync(res, true);
}
}
In the first line of your method, you are replacing the productValues property of request object with a new empty list, :
request.productValues = new List<ProductValuesRequest>();
Therefore, in foreach loop, you are iterating on an empty list.
Remove the first line and see if you still have any issues.
You are assigning an emplty list to productValues as
request.productValues = new List() and trying to iterate the empty list.

Entity Framework Include/select only certain properties from different tables

Let's say I have a method called GetThreadWithComments(). Each thread has 1 user (the creator) and has a list of comments. Each comments has 1 user (the poster).
Here are the classes (generated by EF):
public class Thread
{
public int ThreadId { get; set; }
public int UserId { get; set; }
public string Message { get; set; }
public List<Comment> Comments { get; set; }
public User User { get; set; }
}
public class Comment
{
public long CommentId { get; set; }
public string Message { get; set; }
public int UserId { get; set; }
public int ThreadId { get; set; }
public User User { get; set; }
}
So basically, I want to load a thread with user info, and associated comments with user info. I've tried something like this:
db.Threads.Select(x => new
{
x,
x.User = new { x.User.Username, x.User.Email },
x.Comments = x.Comments.Select(c => new
{
c.Message,
c.CommentId,
c.User = new { c.User.Username, c.User.Email }
})
});
The above does not work. However, I am not too sure on how to correctly do this. I could use include, but that would generate all properties. Since speed is a concern, I am trying to keep things as light as possible.
Reason it does not work: it does not build. Compile time error. The 2 errors I get are:
Cannot implicitly convert type '' to...
and
CS0746 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
First, define entity relationships as virtual, for example
public User User { get; set; }
should be
public virtual User User { get; set; }
Second, in case of the later posted compiler error, try adding the member names.
So instead of
x.User = new { x.User.Username, x.User.Email }
use
x.User = new { Username = x.User.Username, Email = x.User.Email }
Also there is too much x in there. The corrected example would be:
db.Threads.Select(x => new
{
x,
User = new { Username = x.User.Username, Email = x.User.Email },
Comments = x.Comments.Select(c => new
{
c.Message,
c.CommentId,
User = new { Username = c.User.Username, Email = c.User.Email }
})
});
Try this,
var result = db.Threads.Include(thread => thread.Comments);
Hope helps,

linq query to DB returns model with list, when model is ok but list is empty

This drives me crazy I have mode with list of two other models in it. As long as I had there iCollections it worked fine, but I had to change them to List, because I need to export the data into XML.
Model
public class PortalUser
{
//private List<UserToTeam> UserToTeam_ = new List<UserToTeam>();
//private List<Mandays> Mandays_ = new List<Mandays>();
public int ID { get; set;}
public string Name { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
public Role Role { get; set; }
//public virtual ICollection<Mandays> Mandays { get; set; }
public List<Mandays> Mandays { get; set; } //{ return Mandays_; }
[DisplayName("Team")]
//public virtual ICollection<UserToTeam> UserToTeam { get; set; }
public List<UserToTeam> UserToTeam { get; set; } //{ return UserToTeam_; }
public PortalUser()
{
Mandays = Mandays ?? new List<Mandays>();
UserToTeam = UserToTeam ?? new List<UserToTeam>();
}
Then I use this to get data from the DB
List<PortalUser> dbAgent = db.PortalUser.ToList();
End while debugging the result ends as Count = 0. While iCollection return the model within model correctly the List always returns Count = 0. In DB Users have Mandays and even team, but result is always empty.
I am sure I am missing something simple, please help. I can post more code if needed.
I am not sure if EF supports a List collection like that, however you need the virtual keyword in there so EF can create wrapped collections appropriately.

How can I use LINQ to filter and sort rows from one collection to another?

I have the following classes:
public class Menu
{
public int Order { get; set; }
public string Title { get; set; }
public string Status { get; set; }
public string Type { get; set; }
public bool Default { get; set; }
public string Link { get; set; }
public string Notes { get; set; }
public string Text { get; set; }
}
public class MenuItem
{
public int Order { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public bool Default { get; set; }
public string Link { get; set; }
}
Also this which returns ICollection
var menu = _menuRepository.GetPk(pk);
Can someone show me how I can use LINQ to:
a) Get the data from menu
b) Select only rows where Status = "00"
c) Order by Order
d) Put the data into the MenuItem class
I heard there are maybe two ways of coding this. Can anyone explain these and advise which would be better.
Try this:
var menuItems = _menuRepository.GetPk(pk)
.Where(m => m.Status == "00")
.OrderBy(m => m.Order)
.Select(m => new MenuItem
{
Order = m.Order,
Title = m.Title,
Type = m.Type,
Default = m.Default,
Link = m.Link
});
You can throw .ToList() at the end to materialize collection immediately.
Jimmy's answer looks right to me - here's the equivalent in query expression form:
var query = from m in _menuRepoistory.GetPk(pk)
where m.Status == ""
order by m.Order
select new MenuItem
{
Order = m.Order, Title = m.Title, Type = m.Type,
Default = m.Default, Link = m.Link
};
You might want to consider adding a MenuItem constructor which takes a Menu, or a ToMenuItem method to Menu, so you could just use:
var query = from m in _menuRepoistory.GetPk(pk)
where m.Status == ""
order by m.Order
select m.ToMenuItem();
(As with Jimmy's answer, you can call ToList to materialize the results in a List<T>.)

How to use a dictionary property in C# using LINQ

While I userstand how get; & set; with simple types such as strings now can more properties like Dictionary be get or set or can they?
I have a small dos programe trying to do this. snippet below.
# User.cs
namespace LDIFMod
{
public class User
{
public string UserHash { get; set; }
public string UserID { get; set; }
public Dictionary<string, string> UserDict { get; set; } <- how to do this???
}
}
and in my Program.cs
var query = from line in File.ReadAllLines(args[0])
let UserRecord = line.Split(',')
select new User()
{
UserHash = UserRecord[2].Trim() +UserRecord[3].Trim(),
UserID = UserRecord[4].Trim(),
UserDict.???? // userDict should contain key = UserRecord[5] & value = UserRecord[9]
}
You will need to first initialize the dictionary in the constructor of the user class. Use the private set to prevent people from re-initializing the dictionary.
# User.cs
namespace LDIFMod
{
public class User
{
User()
{
UserDict = new Dictionary<string, string>()
}
public string UserHash { get; set; }
public string UserID { get; set; }
public Dictionary<string, string> UserDict { get; private set; }
}
}
Your calling code becomes
var query = from line in File.ReadAllLines(args[0])
let UserRecord = line.Split(',')
select new User()
{
UserHash = UserRecord[2].Trim() +UserRecord[3].Trim(),
UserID = UserRecord[4].Trim(),
UserDict.Add(UserRecord[5],UserRecord[9]);
}
This returns one dictionary per query row. if you want all of the rows to share a dictionary you will need to make it static or not store it inside User. If you do this be aware that linq is delayed execution so the dictionary will not be fully populated until after you fully enumerate the query.
I thought I would give a example of how to do it with all of them in a single dictionary.
# User.cs
namespace LDIFMod
{
public class User
{
public string UserHash { get; set; }
public string UserID { get; set; }
public readonly string[] SourceData {get; private set;}
}
}
and here is the query
var query = from line in File.ReadAllLines(args[0])
let UserRecord = line.Split(',')
select new User()
{
UserHash = UserRecord[2].Trim() + UserRecord[3].Trim(),
UserID = UserRecord[4].Trim(),
SourceData = UserRecord;
}
var UserLookp = query.ToDictionary((user) => user.SourceData[5], (user) => user.SourceData[9]);
This is purely from memory without a ide to check for bugs so there could be some errors.
You can use the collection initializer to create the dictionary:
{
UserHash = UserRecord[2].Trim() +UserRecord[3].Trim(),
UserID = UserRecord[4].Trim(),
UserDict = new Dictionary<string, string> { { UserRecord[5], UserRecord[9] } }
}
Also, see here: http://msdn.microsoft.com/en-us/library/bb531208.aspx

Categories

Resources