export linq query to a text file [closed] - c#

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 7 years ago.
Improve this question
I want to make a text file from a linq query.
The point is that my query produces more than 3000 records each time and when I use foreach it takes 2 minutes to extract all records and fields data.
foreach (var query3 in query)
{
line += (string)query3.Name.Trim() +"\t"+(string)query3.Code.Trim()+"\n";
}
How can I export linq query to a text file or string variable??
my query is something like :
var Query=from c in db.Exp select c;

You shouldn't use string when you perform a lot of string appending. Since string is an immutable object, a new block of memory is allocated in each iteration. You should use StringBuilder instead:
StringBuilder line = new StringBuilder();
foreach (var q2 in query)
{
line.AppendFormat("{0}\t{1}\n", query3.Name.Trim(), query3.Code.Trim());
}
File.WriteAllText(#"...", line.ToString());

Related

How do I turn a list of words into a flattened list of characters in C#? [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 1 year ago.
Improve this question
I have a list of string like this:
List<string> KeyList=["AD","B","KT"]
I want to split each item in the list in such a way where the final list output is a list of strings with only 1 character.
e.g.
["A","D","B","K","T"]
I have tried doing KeyList.ToCharArray() but that does not work.
You could use the Select LINQ extension method to turn each string into a list of 1-character strings, then flatten the list with SelectMany:
var list = new List<string>{ "AB", "B", "KT" };
var newList = list.SelectMany(s => s.Select(c => new string(c, 1))).ToList();

Assign a list of values to another list using linq 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 2 years ago.
Improve this question
I want to achieve the same code logic using Linq basically or re factoring the code would be great.
List<PublisherDto> listOfMappedPublishers = new List<PublisherDto>();
listOfPublishers.ForEach(pub =>
{
PublisherDto publisher = new PublisherDto();
publisher.ApiKey = pub.ApiKey;
publisher.Name = pub.Name;
publisher.Password = pub.Password;
listOfMappedPublishers.Add(publisher);
});
return listOfMappedPublishers;
You can use Select and ToList as follow:
List<PublisherDto> listOfMappedPublishers = listOfPublishers.Select(i=> new PublisherDto()
{
ApiKey = i.ApiKey,
Name = i.Name,
Password = i.Password
}).ToList();

Refactor code to make 3 size array dynamic in code [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 3 years ago.
Improve this question
I want to refactor the code, I need to make this code dynamic, how to do that, see below part?
public class Trv34
{
public string CustomerNumber
public string ProductName
}
Models.Trv34[] itemTrv34 = new Models.Trv34[3]
itemTrv34[0] = new Models.Trv34 {CustomerNumber ="1", ProductName="p1"}
itemTrv34[1] = new Models.Trv34 {CustomerNumber ="2", ProductName="p2"}
itemTrv34[2] = new Models.Trv34 {CustomerNumber ="3", ProductName="p3"}
request.VODB.Trv34= new Models.Trv34[] {
itemTrv34[0],
itemTrv34[1],
itemTrv34[2],
};
I want to replace this part with something dynamic instead
itemTrv34[0],
itemTrv34[1],
itemTrv34[2],
The easiest way that comes to mind is to use Linq:
request.VODB.Trv34 = itemTrv34.ToArray();

Compare DB Item with an List<string> Input [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 4 years ago.
Improve this question
This is the data from my input
{Test,Sample}
This is the data from my database
{C:\Users\Test,D:\DriveB\Sample\Test,C:\Users\Private\Item\LocationB}
My expected output is
{C:\Users\Test,D:\DriveB\Sample\Test}
How can I get results out using that input? So far I have tried using a for loop like this
var Count = Input.Count;
for(var i = 0; i < Count; i++)
{
data = data.Where(u => u.Location.Contains(Input[i])).ToList();
}
The variable data has already been extracted from the database and has a format like this
Id
Name
Location
FileType
But the problem is that when it goes past the first loop, it immediately eliminates the second inputs data.
var Count = Input.Count;
data = data.Where(x => input.Any(y => x.Location.Contains(y))).ToList();
}
would likely work. The use of Any means:
Get me any row from data where at least one of the entries from input is contained in (i.e. a substring of) the Location.
Use split by "," first
var input = new List<String>() { "Test", "Sample" };
var db = #"C:\Users\Test,D:\DriveB\Sample\Test,C:\Users\Private\Item\LocationB";
var result = db.Split(',').Where(p => input.Any(c => p.Contains(c))).ToList();

how to check whether dropdown list has all items in the given 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 9 years ago.
Improve this question
I have got a requirement like I have got list of strings and I got dropdownlist items now we need to check whether the dropdownlist has all items in the give list of strings or not and i need to return the bool condition ...
for that purpose i have done like this ....
public static bool GetMacthedItems(DropDownList ddllist1, DropDownList ddllist2, string MatchedItem1, string MatchedItem2, string MatchedItem3)
{
bool results =true;
List<String> list1 = new List<String> {MatchedItem1, MatchedItem2, MatchedItem3};
if (ddllist1.SelectedValue.ToString() == MatchedItem1 && (list1.Any(x => x.Contains(ddllist2.SelectedValue.ToString()))))
{
results = false;
}
return results;
}
but the above condition is checking like whether the dropdownlist selected value is in that list or not ....
i want entire like all items in dropdownlist are in that list or not
would any one pls help on this ....
var allContained = ddlCountry.Items.Cast<ListItem>().Select(item => item.Value).All(item => lst.Contains(item.ToString());
Try this - for performance it will stop iterating once the condition is broken and return false by using a combination of All with Contains.

Categories