简体   繁体   English

Linq-如何在Linq中获取查询结果并将其添加到数组

[英]Linq - How to take the result for a query in Linq and add it to an Array

I use c#, linq and EF4 I would like ask your help. 我使用c#,linq和EF4,我想向您寻求帮助。

My question: I have a linq query, but i need the result for this query be inserted in an array string. 我的问题:我有一个linq查询,但是我需要将此查询的结果插入数组字符串中。 Value added should be Title and ContentId (ContentId from EF is an INT but i need it as a string) 添加的值应该是Title和ContentId(来自EF的ContentId是一个INT,但我需要它作为字符串)

Please let me know, many thanks in advances!PS: Please post the full code :-) 请让我知道,在此先感谢您!PS:请发布完整代码:-)

       public static string[] GetCompletionList(string prefixText, string count)
{
    using (CmsConnectionStringEntityDataModel context = new CmsConnectionStringEntityDataModel())
    {
    var queryTitle = (from content in context.CmsContents
                      select new
                      {
                          Title = content.Title, // String
                          ContentId = content.ContentId.ToString() // Int
                      }).ToArray();
    return queryTitle;


    }

If you want to have ContentId as a string, then do this: 如果要使用ContentId作为字符串,请执行以下操作:

var queryTitle = (from content in context.CmsContents
                 select new
                 {
                     Title = content.Title, // String
                     ContentId = content.ContentId.ToString() // Int
                 }).ToArray();

queryTitle will be an array of the anonymous type created, which has two properties: queryTitle将是创建的匿名类型的数组,该数组具有两个属性:

  1. Title 标题
  2. ContentId ContentId

Both of type string. 两者均为字符串类型。

If you don't want to have an array of the anonymous type, but an array of strings, then use this code: 如果您不想有一个匿名类型的数组,而是一个字符串数组,请使用以下代码:

var queryTitles = (from content in context.CmsContents
                 select "Title: " + content.Title + ", ContentId: " +  content.ContentId.ToString()).ToArray();

if you are looking to have contentID and Title in one big array of string (from your question, it sounds like that but not very clear), you might want to try this 如果您希望在一大串字符串中包含contentID和Title(从您的问题来看,听起来像是这样,但不是很清楚),您可能想尝试一下

var resultArray = titlesAsArray
            .Select(a => new[]
                             {
                                 a.ContentId.ToString(), a.Title
                             })
            .SelectMany(x => x).ToArray();

or to modifiy your original query 或修改您的原始查询

var resultArray = context.CmsContents.Select(content => new[]
                                 {
                                     content.ContentId.ToString(), content.Title
                                 }).SelectMany(content => content).ToArray();
var strings = from content in context.CmsContents
              select string.Format ("{0} {1}",
                  content.ContentId,
                  content.Title
              );

That should help you. 那应该对你有帮助。
Note that it's a common misconception that you need an array to work with query results—in fact, you don't, the result is already IEnumerable . 请注意,这是一个常见的误解,您需要一个数组来处理查询结果-实际上,您不需要,结果已经是IEnumerable If you're sure that you need an array, just wrap query in parentheses and call ToArray extension method on the result. 如果确定需要一个数组,只需将查询括在括号中并在结果上调用ToArray扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM