none
Read multiple columns in one column RRS feed

  • Question

  • How can I pass multiple column names here.

    like below

    System.Data.Linq.Mapping.

    ColumnAttribute(Storage="_id,_name,_dob" ...)

    Then I can take value in dictionary object and do some process.


    • Edited by ather_13 Friday, December 23, 2011 5:04 PM
    Friday, December 23, 2011 5:03 PM

Answers

  • hi My Friend

    You Can Combine Field in select part of query experssion.

    if you cant use this help please alert to me

     

    Sunday, December 25, 2011 12:23 PM
  • Hi ather_13,

    I'm afrain I don't think it can works. When a call is made to a stored procedure or a function that returns a scalar value, linq to SQL designer generated code calls ExecuteMethodCall which returns an object that implements of IExecuteResult. But Dictionary<TKey,TValue> doesn't implement this interface.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 28, 2011 3:54 AM
    Moderator

All replies

  • hi My Friend

    You Can Combine Field in select part of query experssion.

    if you cant use this help please alert to me

     

    Sunday, December 25, 2011 12:23 PM
  • Hi ather_13,

    Welcome to MSDN Forum.

    I agree with hamedsahami, you can just combine the fileds when you query them out, you can also take the value into dictionary.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 26, 2011 3:04 AM
    Moderator
  • Thanks hamedsahami & Allen for your responses.

    Allen,

       Are you telling me is there a way to directly take value in dictionary instead of taking value in single result object ?

    if yes then can you tell me how ?

    Tuesday, December 27, 2011 2:34 PM
  • Hi ather_13,

    You can do like this

                TestDataContext context = new TestDataContext();
                var query = from b in context.GetTable<Book>() select new { key = b.BookID, value = b.BookID + b.BookName + b.Price + b.PublishDate };
                Dictionary<int, string> dic = new Dictionary<int, string>();
                foreach (var v in query)
                {
                    dic.Add(v.key, v.value);
                }
    

    This is a simple sample, you can use the object's id as the dictionary's key and concatenate all the properties as the dictionary's value.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 28, 2011 1:51 AM
    Moderator
  • Thanks for your reply Allen.

    Actually what I want instead of getting IExecuteResult, I need dictionary object. See below example, Can it be possible ?

    ------------------------------------------------------------------------------------------------------------------------

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetProducts")]
    public ISingleResult<Products> GetProducts()
    {
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
    return ((ISingleResult<Products>)(result.ReturnValue));
    }

    ------------------------------------------------------------------------------------------------------------------------

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetProducts")]
    public Dictionary<string,dynamic> GetProducts()
    {
    Dictionary<string,dynamic> dic = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
    return dic;

    }

    Wednesday, December 28, 2011 3:05 AM
  • Hi ather_13,

    I'm afrain I don't think it can works. When a call is made to a stored procedure or a function that returns a scalar value, linq to SQL designer generated code calls ExecuteMethodCall which returns an object that implements of IExecuteResult. But Dictionary<TKey,TValue> doesn't implement this interface.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 28, 2011 3:54 AM
    Moderator
  • ok so is there any option to get dynamic type object, So later on I can retrieve column name & its value thorugh linq query ?

    or is there any other option ?

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetProducts")]
    public ISingleResult<dynamic> GetProducts()
    {
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
    return ((ISingleResult<dynamic>)(result.ReturnValue));
    }

     

     

    Wednesday, December 28, 2011 2:13 PM
  • Hi ather_13,

    I know what you want, I replied your another case about this issue. The link below can help you to retireve more than one value from the stored procedure,

    http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

    But when you use the result, you have to know the definitely type of the object.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, December 29, 2011 2:25 AM
    Moderator