none
Linq query question RRS feed

  • Question

  • I wanna use linq to do such a query :

    select all filed except one field from table

    for example : In this table , I need select all field except FieldA
    Table A
    {
    ID,
    FieldA,
    FieldB,
    FieldC,
    }


    How can I wirte the linq to do it  ?

    I know this can do ,but If TableA Has more than 100 field , this code will be ugly .

    var a =from b in TableA
    select new
    {
    b.ID,
    b.FileldB,
    b.FileldC,
    }


    Thanks for any good advice .Thanks.
    Friday, August 22, 2008 3:25 AM

Answers

  • I think I understand the question now.

     

    Let's say you have type A and want to return list of type B

     

    var query = from a in CdataContext.A

                     select new B(a.Field1, a.Field2);

    // query is IQueryable<B>

    List<B> = query.ToList();

     

    If that isn't working look at Cast<T> extension to help conversion to specific type.

    Tuesday, August 26, 2008 9:12 AM
    Answerer

All replies

  •  

    >  If TableA Has more than 100 field

     

    If that's how big the table is, you can expect ugly code!

     

    Seriously, look at creating Expressions in code. If your field names are logical e.g. FieldA, FieldB etc. you can write a loop to select them using Expressions. See http://msdn.microsoft.com/en-us/library/bb397951.aspx and search for articles about writing expressions using code.

     

    e.g.

    Code Snippet

    // create parameter expression

    var paramExpr = Expression.Parameter(typeof(object), "p");

    var selectList = new List<Expression>();

    for (int i = 0; i < 26; i++)

      {

        string fieldName = string.Format("field{0}", Convert.ToChar(i + 65));

        // create an expression

        Expression fieldExpr = Expression.Property(paramExpr, fieldName);

        selectList.Add(fieldExpr);

      }

    // TODO: generate LINQ statement using selectList

     

     

    Friday, August 22, 2008 11:08 AM
    Answerer
  • Thanks for your solution .
    I need explain it in more detail .
    If I use this linq sentence :

    var a =from b in CdataContext.TableA
              select b;
    List<TT> d=a.ToList();

    I need the List<TT> not any var type.

    In this scenario ,If I try to select all filed except one (here is FieldB) . like linq code is :

    var a =from b in CdataContext.TableA
    select new
    {
    FieldA,
    FieldC,
    ....etc
    };


    Then the a.ToList() object is not List<TT>

    Does anyone know how can I wirte the linq ,then the result of a.ToList() will still List<TT> type .

    Thanks .

    Friday, August 22, 2008 1:08 PM
  • List<T> is C# for "a generic list of type T" - where T can be any type, e.g. String, Object, Bitmap etc.

     

    If you don't know what generics are then you're not really going to understand LINQ or any solution I try to explain.

     

    Start here: http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx
    Friday, August 22, 2008 3:24 PM
    Answerer
  • If you want ,
    you can wirte the code use List<T>  to solve my problem which I mentioned .

    One question , if I get the result of List<T> type . How the client can know which T Type is what ?
    Because client app use databinding which need know the specific member name of  specific type..

    Saturday, August 23, 2008 2:31 AM
  • If you want to pass the results of a LINQ query back to a calling function e.g. the UI you have two choices:

     

    1) return a typed list, e.g List<MyType> - you have to create a class/structure and return a list of this type

     

    2) return a list of an anonymous type - in which case you need to use reflection and know what property names to access.

     

    Monday, August 25, 2008 3:57 PM
    Answerer
  • I think I understand the question now.

     

    Let's say you have type A and want to return list of type B

     

    var query = from a in CdataContext.A

                     select new B(a.Field1, a.Field2);

    // query is IQueryable<B>

    List<B> = query.ToList();

     

    If that isn't working look at Cast<T> extension to help conversion to specific type.

    Tuesday, August 26, 2008 9:12 AM
    Answerer