Why nested foreach in linq works after all operations? [closed] - c#

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 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
Please tell me why in this case the last instruction from the Combine (Where) method will work before the logic in foreach (Bar method). The example is completely artificial.
public class TestLinq
{
public static class Foo
{
public static IEnumerable<FooObject> Bar(ICollection<FooObject> forrObjects)
{
return forrObjects
.Where(fooObject => AnyConditions(fooObject))
.GroupBy(fooObject => fooObject.AnyProp)
.SelectMany(foofData =>
{
foreach (var foo in foofData)
{
foo.Flags |= TestFlag;
}
return new[]
{
new BarObject(foofData.First()),
new BarObject(foofData.First()),
};
});
}
}
public FooObject[] Combine(ICollection<FooObject> fooObjects)
{
return fooObjects
.Union(Foo.Bar(fooObjects))
.Where(foo => !foo.Flags.HasFlag(TestFlag))
.ToArray();
}
}
I used debug and found out the order of execution, I want to understand why it works this way

Related

Add content to list [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 2 years ago.
Improve this question
How can I proceed to add in the list the content of my foreach, without deleting the previous content?
lSave.Add(NextKey, new LSave());
foreach (var container in corpse.containers)
lSave[NextKey].items = GetItems(container).ToList();
And the Structure
public Dictionary<uint, LSave> lSave = new Dictionary<uint, LSave>();
public class LSave
{
public List<LItems> items;
public LSave() { }
}
public class LItems
{
public string name;
}
IEnumerable<LItems> GetItems(ItemContainer container)
Thanks
If you want to add more items to a specific key in the dictionary, try this:
foreach (var container in corpse.containers)
{
LSave item;
if (!lSave.TryGetValue(NextKey, out item))
{
item = new LSave();
item.items = new List<LItems>();
lSave.Add(NextKey, item);
}
item.items.AddRange(GetItems(container));
}
It adds an LSave to NextKey if needed and then adds the result of GetItems(container) to the items field of that LSave.

C# how does this code work? [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 5 years ago.
Improve this question
Public class form1
{
List<Data> dataInfo = new List<Data>();
private void UpdateBinding()
{
supplierBox.DataSource = dataInfo;
supplierBox.DisplayMember = "Supplier";
}
}
I have another class called Data:
public class Data
{
public string Supplier { get; set; }
}
This is able to update my textBox named supplier on Visual Studio with data from my SQL Server that I have grabbed. How would I access the data from my SQL Server if I just wanted to put it in a variable like var?
supplier.Text = dataInfo.FirstOrDefault().Supplier;
would place the first element from your list of suppliers into your text box named "suppliers". If you don't have any items in your list of dataInfo then you would get a NullReferenceException.

Automatically apply C# 7 getter and setter style [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Is there any way to automatically apply C# 7 getter and setter styles (or maybe other new language features)?
It would be nice if there would be any way to automatically change properties like these:
public string MyProperty1
{
get
{
return this.myProperty1;
}
}
public string MyProperty2
{
get
{
return this.GetSomething();
}
set
{
this.SetSomething(value);
}
}
public string MyProperty3
{
get
{
return this.myProperty3;
}
set
{
this.myProperty3 = value;
this.RaisePropertyChange(nameof(MyProperty3));
}
}
to this:
public string MyProperty1 => this.myProperty1;
public string MyProperty2
{
get => this.GetSomething();
set => this.SetSomething(value);
}
public string MyProperty3
{
get => this.myProperty3;
set
{
this.myProperty3 = value;
this.RaisePropertyChange(nameof(MyProperty3));
}
}
Maybe there is an extension which can handle this task =)
Thanks everybody in advance!
Use Resharper for this. Resharper

List that contains list that contains list and so on in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
This is code:
public class Comment
{
public DateTime CreateAt { get; set; }
public Comment ParentComment { get; set;}
public List<Comment> SubComments { get; set; }
public string Text { get; set; }
}
I'am trying to do something like on facebook. You can comment every single comment, so there will be like a tree of comments.. I'am having trouble with trying to display all the comments, precisely text. I can't figure out how to do that. If anyone could help I'll appreciate it!
You can use recursion for process each comment and their sub comments for example:
public void checkComment(Comment comment)
{
//Check if the comment is valid
if (comment != null)
{
//Do whatever you want to do with your comment for example print to console
Console.WriteLine(String.Format("Comment: {0}", comment.Text));
//Check if i have any sub comments
if (comment.SubComments.Count > 0)
{
//Process each sub comment (recursive)
comment.SubComments.ForEach(x => checkComment(x));
}
}
}

Create buttons for this dynamic values [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 7 years ago.
Improve this question
I have a controller and controller returns some dynamic values.I need to create buttons for that dynamic values.
My Contoller
public ActionResult LoginAs()
{
string[] roles = (string[])TempData["data"]; // this array returns 3 values.I need to create a buttons for that values.
return View();
}
I have no idea how to create a buttons for this dynamic values.mvc
UPDATED
(Buttons type changed to string[])
Model
public class LoginAsViewModel
{
public string[] Buttons {get;set;}
}
Controller
public ActionResult LoginAs()
{
//I don't know what TempData returns
string[] roles = (string[])TempData["data"];
var model = new LoginAsViewModel
{
Buttons = (string[])TempData["data"]
};
return View(model );
}
View
#model LoginAsViewModel
#for(int i=0; i < Model.Buttons.Count(); i++)
{
<button>#Model.Buttons[i]</button>
}

Categories

Resources